我知道这个主题上有很多问题,但似乎没有一个答案可以解决我的所有要求。
我的要求:
我想使用子流程模块。我已经尝试使用选项CREATE_NEW_CONSOLE
subprocess.Popen(("git", "status"), close_fds=True, creationflags=subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW, shell=True)
但是:
x-terminal-emulator -e "bash -c \"git status; exec bash\""
将打开默认终端的新窗口并运行命令
bash -c "git status; exec bash"
在那个窗口。在Python中,它看起来像
subprocess.Popen('x-terminal-emulator -e "bash -c \\"git status; exec bash\\""', shell=True)
最后运行“ exec bash”会在控制台窗口中打开一个新的shell,并防止其立即退出。根据this answer,在Windows上,您可以使用“ start cmd / k”,然后运行命令。我没有尝试过,但大概是
subprocess.Popen('start cmd /k git status', shell=True)
将解决问题。