subprocess.call 打开终端就闪退了? (Windows)

subprocess.call open terminal and just flahing out? (Windows)

我一直在 PC/Windows 中尝试这个 subprocess.call,想知道如何让它工作?感谢您的帮助和提示。 (我一直在搜索,但还没有找到任何相关内容...)

所以这是我的 Python shell 中的代码:(我在新打开的 terminal 中键入 beef 时一切正常。然后是终端window 只是闪烁 out/disappearing?)

>>> import subprocess

>>> subprocess.call('python check_words.py')
0  

# A new window terminal opening and prompt as the check_words.py asking for a word:
What is the word:
beef                     # my typing

# window terminal disappearing?  
# my python shell window exit code `0`   meaning `ok`

您可以使用两种方法来实现此目的 subprocess.Popensubprocess.call

Popen 不会阻塞,允许您在进程处于 运行 时与其交互,或者继续您的 Python 程序中的其他事情。调用 Popen returns 一个 Popen 对象

call 会阻塞。虽然它支持与 Popen 构造函数相同的所有参数,因此您仍然可以设置进程的输出、环境变量等,您的脚本等待程序完成,并且 call returns 代码表示进程的退出状态

from sys import executable
from subprocess import Popen, CREATE_NEW_CONSOLE

Popen([executable, 'check_words.py'], creationflags=CREATE_NEW_CONSOLE)
input('Enter to exit')

或者您可以将字符串参数与关键字参数 shell = True 一起使用,但这是一种危险的做法:implications

Popen('check_words.py',shell = true, creationflags=CREATE_NEW_CONSOLE)

你也可以用 call 试试,结果也是一样的