我有一个带有 GUI 的程序,它通过 Popen 调用运行外部程序:
I have a program with a GUI that runs an external program through a Popen call:
p = subprocess.Popen("<commands>" , stdout=subprocess.PIPE , stderr=subprocess.PIPE , cwd=os.getcwd()) pmunicate()但是无论我做什么,都会弹出一个控制台(我也试过将 NUL 传递给文件句柄).有没有办法在不获取我调用的二进制文件以释放其控制台的情况下做到这一点?
But a console pops up, regardless of what I do (I've also tried passing it NUL for the file handle). Is there any way to do that without getting the binary I call to free its console?
推荐答案来自这里:
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,如下所述:Python 作为 Windows 服务运行:OSError: [WinError 6] 句柄无效
Note that sometimes suppressing the console makes subprocess calls fail with "Error 6: invalid handle". A quick fix is to redirect stdin, as explained here: Python running as Windows Service: OSError: [WinError 6] The handle is invalid
更多推荐
在没有控制台的情况下使用 Popen 在 pythonw 中运行进程
发布评论