使用Popen在没有控制台的情况下在pythonw中运行进程

sbi*_*rch 17 python console user-interface popen pythonw

我有一个带有GUI的程序,通过Popen调用运行外部程序:

p = subprocess.Popen("<commands>" , stdout=subprocess.PIPE , stderr=subprocess.PIPE , cwd=os.getcwd())
p.communicate()
Run Code Online (Sandbox Code Playgroud)

但是无论我做什么,都会弹出控制台(我也尝试将NUL传递给文件句柄).如果没有得到我调用的二进制文件来释放它的控制台,有没有办法做到这一点?

int*_*jay 31

这里:

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"])
Run Code Online (Sandbox Code Playgroud)


Lia*_*iam 9

做就是了subprocess.Popen([command], shell=True)