How to avoid console window with .pyw file containing os.system call?

后端 未结 6 1085
既然无缘
既然无缘 2020-11-29 08:59

If I save my code files as .pyw, no console window appears - which is what I want - but if the code includes a call to os.system, I still get a pes

6条回答
  •  借酒劲吻你
    2020-11-29 09:32

    People are a bit lazy... I would thx @Piotr Dobrogost and @Frank S. Thomas for their answers.

    I came with this code who is runinng on Linux and Windows:

    import platform
    import subprocess
    startupinfo = None
    if platform.system() == 'Windows':
        import _subprocess  # @bug with python 2.7 ?
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
        startupinfo.wShowWindow = _subprocess.SW_HIDE
    

    Later...

    args = [exe, ...]
    out = subprocess.check_output(args, startupinfo=startupinfo)
    

    Thx guys ;)

    Additionally: just to note that the following code using 'call' also works on Python 2.7 (on Windows) with the 'startupinfo' code above:

    def run_command(cmd, sin, sout):
        print "Running cmd : %s"%(" ".join(cmd) )
        return subprocess.call( cmd, stdin=sin, stdout=sout, startupinfo=startupinfo)
    

提交回复
热议问题