如何使用Python在后台运行DOS批处理文件?如何使用Python在后台运行DOS批处理文件?
我有一个test.bat文件在说C:\
现在,我想运行这个bat文件在后台使用python,然后我想返回到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.)
>>>
顺便说一句,当你想要字符串中的文字反斜线字符(例如指定Windows文件路径)时,您必须将它们加倍为“C:\\ test.bat”,或者通过在字符串前加字母来指示原始字符串格式在'r'C:\ test.bat'这样的第一个引号之前的'r'。请参阅文档中的[字符串文字](http://docs.python.org/reference/lexical_analysis.html#string-literals)。 – martineau