0

I'm trying to make a terminal while using Replit (a collaborative browser based IDE) that is using Ubuntu 5.13.12. But, for some reason subprocess doesn't accept lists/tuples into running it. I tried using with os.system but i realized it only returns a value if it's using a string.

The code shown above only works in distros of Linux like Ubuntu, Fedora, Debian.

Python Simplified Terminal

import termcolor;
import subprocess;
import os;

while (__name__ == '__main__'):
    _DEPENDENT_PROCESS = []
    _STRING_DIVISOR = " "

    terminal_input = input(termcolor.colored(os.path.dirname(os.path.realpath(__file__)), "blue", attrs=['bold']) + "$ ")
    if _STRING_DIVISOR in terminal_input:
      counted_arguments = terminal_input.count(_STRING_DIVISOR)
      splited_arguments = list(terminal_input.split(_STRING_DIVISOR, counted_arguments))
      _DEPENDENT_PROCESS, error = splited_arguments, None
    intrepeter = subprocess.run([_DEPENDENT_PROCESS], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    _terminal_output = intrepeter.stdout
    print(_terminal_output.decode('ascii'))
Error: Expected str, bytes, os.Paths, not lists

1 Answer 1

1
import termcolor;
import subprocess;
import os;

while (__name__ == '__main__'):
    terminal_input = input(termcolor.colored(os.path.dirname(os.path.realpath(__file__)), "blue", attrs=['bold']) + "$ ")
    intrepeter = subprocess.run(terminal_input.split(" "), stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    _terminal_output = intrepeter.stdout
    print(_terminal_output.decode('ascii'),end="")

What's wrong with doing this? It works the way a terminal would for me. Although I know it doesn't answer your question.

Sign up to request clarification or add additional context in comments.

4 Comments

The problem is when you execute a command like echo “Hello World” it simply doesn’t work
Check the answer again, I updated my mistake. I'm splitting the string which returns a list. You can avoid all of this work by using shell=True, but you should probably shlex the output first.
That makes more sense, thanks for your answer
I would recommend using something like the autocomplete.py script I'm using in this project of mine github.com/mustansirgodhrawala/rst/blob/master/src/rst/… it'll help you catch arrow keypresses and tabs for autocomplete if you want it. Although you will need some funky implementation for quotes in this solution I've provided. I might do one today with the stuff weird for loops if you want it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.