从这里:
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]句柄无效