Saturday, 6 November 2021

Python subprocess make a new window and keep executing commands in it

I want to use subprocess.Popen or any alternative to spawn a new Terminal window and keep supplying commands to it whose output is displayed in the same terminal window. So far I have tried

import subprocess
i=subprocess.Popen("start cmd /K tree",creationflags=subprocess.CREATE_NEW_CONSOLE,stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
i.stdin.write(b"echo hi\n")

out=i.stdout.read()
print(out)

However only the first command tree executes and the echo does not executeHere it runs tree and stops there after closing the terminal window Here it runs tree and stops there after closing the terminal window the program ends without executing the echo command



from Python subprocess make a new window and keep executing commands in it

No comments:

Post a Comment