如何使用 Python 在后台运行 DOS 批处理文件?
我在 C:\
中有一个 test.bat 文件
现在,我想在后台使用 python 运行这个 bat 文件,然后我想返回到 python 命令行。
我从 python 命令行使用 subprocess.call('path\to\test.bat')
运行批处理文件。
它在与 python 命令行相同的窗口中运行批处理文件。
如果还不清楚/TL.DR-
发生了什么:
>>>subprocess.call('C:\test.bat')
(Running test.bat. Can't use python in the same window)
我想要的:
>>>subprocess.call('C:\test.bat')
(New commandline window created in the background where test.bat runs in parallel.)
>>>
最佳答案
这似乎对我有用:
import subprocess
p = subprocess.Popen(r'start cmd /c C:\test.bat', shell=True)
p.wait()
print 'done'
关于python - 如何使用 Python 在后台运行 DOS 批处理文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12793760/