如何从python脚本中启动一个批处理文件,并从中分离 [英] How to start a batch file from within a python script and detach from it

查看:227
本文介绍了如何从python脚本中启动一个批处理文件,并从中分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要启动一个的.bat 文件python脚本。在的.bat 文件将无限期运行,并且最好,永不再来。

I have a python script that needs to start a .bat file. The .bat file runs indefinitely, and, ideally, never returns.

有一个运行可执行国外没有问题或的.bat 从python脚本中,但所有的实现,我发现来看,它作为一个孩子,并等待它返回。

There's no problem running a foreign executable or .bat from within a python script, but all the implementations I've found run it as a child and wait for it to return.

我可以运行一个外部脚本,并从中分离,允许它运行欢快,并有python脚本继续运行?

Can I run an external script and detach from it, allowing it to run merrily, and have the python script continue to run?

*的编辑:* 的使用 POPEN 运行在相同的命令提示符窗口中的批处理文件 - 作为批处理文件永远不会返回时,Python应用程序我们无法继续运行。有没有办法在新的命令提示符窗口中运行的批处理文件?

* * using Popen runs the batch file in the same command prompt window - as the batch file never returns, the python app us unable to continue running. Is there a way to run a batch file in a new command prompt window?

推荐答案

您可以使用子:

import subprocess
subprocess.Popen(["mybatchfile.bat"])

这将启动批处理文件并不会等待完成。如果您想验证是否已创建过程中,你可以运行:

This starts the batch file and will not wait for to complete. If you want to verify that process is created, you can run:

p=subprocess.Popen(["mybatchfile.bat"])
if p.pid > 0:
   print "Process created with PID:", p.pid

这篇关于如何从python脚本中启动一个批处理文件,并从中分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆