I am trying to write a basic terminal emulation script, because for some reason I do not have access to the terminal on my mac. But for writing scripts for the game engine in a blender, the console is important, which usually opens in the terminal from which you started working with the blender. To perform simple actions such as deleting, renaming, etc. I used commands using stream = os.popen(command) and then print (stream.read()) . This works great for most things, but not for anything interactive.
Soon I discovered a new way:
sp = subprocess.Popen(["/bin/bash", "-i"], stdout = subprocess.PIPE, stdin = subprocess.PIPE, stderr = subprocess.PIPE) , and then print(sp.communicate(command.encode())) . That should spawn an interactive shell that I can use as a terminal, right?
But in any case, I cannot keep the connection open, and using the last example, I can call sp.communicate once, giving me the following output (in this case for "ls /") and some errors:
(b'Applications\n[...]usr\nvar\n', b'bash: no job control in this shell\nbash-3.2$ ls /\nbash-3.2$ exit\n') . The second time it gives me ValueError: I/O operation on closed file. Sometimes (for example, for "ls") I get only this error: b'ls\nbash-3.2$ exit\n' .
What does it mean? How can I emulate a terminal using python that allows me to control an interactive shell or launch a blender and communicate with the console?
source share