rit*_*att 2 python command-line subprocess
如何使用Python在后台运行DOS批处理文件?
我在说C:\的地方有一个test.bat文件。
现在,我想在后台使用python运行此bat文件,然后返回到python命令行。
我subprocess.call('path\to\test.bat')从python命令行运行批处理文件。它在与python命令行相同的窗口中运行批处理文件。
如果仍然不清楚/ TL.DR-
怎么了:
>>>subprocess.call('C:\test.bat')
(Running test.bat. Can't use python in the same window)
Run Code Online (Sandbox Code Playgroud)
我想要的是:
>>>subprocess.call('C:\test.bat')
(New commandline window created in the background where test.bat runs in parallel.)
>>>
Run Code Online (Sandbox Code Playgroud)
这似乎为我工作:
import subprocess
p = subprocess.Popen(r'start cmd /c C:\test.bat', shell=True)
p.wait()
print 'done'
Run Code Online (Sandbox Code Playgroud)