python - 如何使用 Python 在后台运行 DOS 批处理文件?

标签 python command-line subprocess

如何使用 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/

相关文章:

python - 如何从 seaborn distplot fit= 获取拟合参数?

python - 使用子进程的 PIPE 时如何以字符串形式获取 python 对象 <class '_io.TextIOWrapper' > 的内容?

python - Fastapi 数据库测试隔离与 starlette 配置

python - 无法在 Ubuntu 16.04 上的 python 中使用 OpenCV

python - 正则表达式模式匹配逗号分隔值,逗号周围允许有空格

python错误代码处理

python - 使用 python 记录文件和进程超时的输出

php - Windows 上的 PHP 诅咒

xml - 使用 Saxon 9.6HE 自动执行多个文件的 xquery

bash - 当一组并行命令成功时如何运行命令?