How to run external executable using Python?

后端 未结 4 1276
情话喂你
情话喂你 2020-12-15 19:03

I have an external executable file which i am trying to run from a Python script. CMD executable runs but without generating output. Probably it exit before output can be ge

4条回答
  •  轮回少年
    2020-12-15 19:24

    The os.system method is depreciated and should not be used in new applications. The subprocess module is the pythonic way to do what you require.

    Here is an example of some code I wrote a few weeks ago using subprocess to load files, the command you need to use to delay exit until data has been received and the launched program completes is wait():

    import subprocess
    
    cmd = "c:\\file.exe"
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, creationflags=0x08000000)
    process.wait()
    

    creationflags=0x08000000 is an optional parameter which suppresses the launch of a window, which can be useful if the program you are calling does not need to be directly seen.

提交回复
热议问题