3 回答

TA贡献1772条经验 获得超5个赞
从这里:
import subprocess
def launchWithoutConsole(command, args):
"""Launches 'command' windowless and waits until finished"""
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
return subprocess.Popen([command] + args, startupinfo=startupinfo).wait()
if __name__ == "__main__":
# test with "pythonw.exe"
launchWithoutConsole("d:\\bin\\gzip.exe", ["-d", "myfile.gz"])
请注意,有时抑制控制台会使子流程调用失败,并显示“错误6:无效的句柄”。一种快速修复是重定向stdin,如此处所述:以Windows服务运行的Python:OSError:[WinError 6]句柄无效

TA贡献2051条经验 获得超10个赞
根据Python 2.7文档和Python 3.7文档,您可以Popen通过设置影响创建过程的方式creationflags。该CREATE_NO_WINDOW标志尤其对您有用。
variable = subprocess.Popen(
"CMD COMMAND",
stdout = subprocess.PIPE, creationflags = subprocess.CREATE_NO_WINDOW
)
添加回答
举报