PHi*_*HiL 4 python executable subprocess popen
在 Python 脚本中,我尝试执行以下事件序列:
这是我到目前为止所拥有的:
subprocess.popen(['cmd','/c',r'programThatRuns.exe'])
subprocess.wait() # ? subprocess.check_call()? kill?
subprocess.popen(['cmd','/c',r'otherProgramThatRuns.exe'])
Run Code Online (Sandbox Code Playgroud)
所以我想我真的被困在第二行了
我想你所需要的只是:
subprocess.check_call(['programThatRuns.exe'])
subprocess.check_call(['otherProgramThatRuns.exe'])
Run Code Online (Sandbox Code Playgroud)
该check_call函数将运行程序并等待其完成。如果失败(非 0 退出代码),它将抛出异常CalledProcessError。
您通常不想通过 运行程序cmd,只需直接运行它们即可。仅当程序不是可执行文件时才需要强制使用 cmd,例如对于诸如dir、.bat或.cmd文件之类的内置命令,或者如果您想使用文件关联。