python非阻塞式后台运行bat
首先,bat脚本要实现后台运行,代码如下:
c:\users\linuxbugs\desktop\demo\run_demo.bat
@echo off
if "%1" == "h" goto begin
mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit
:begin
python %cd%\main.py然后我用python调用该脚本,并置于后台,不阻塞python继续向下运行
import os
def run():
os.chdir(r'c:\users\linuxbugs\desktop\demo')
os.popen('run_demo.bat')
if __name__ == '__main__':
run()
print("xxxxxx") # 会直接打印 xxxxxx run函数并不会阻塞python运行bat脚本,并传递txt文件参数
该方法好处:无需权限
若只运行bat脚本
subprocess.call(path + '\\合并.bat', shell=true)
因为文件运行和bat、txt文件不是在同一个目录,所以需要加上路径
shell=true 参数告诉subprocess模块在shell中运行脚本
如果需要传递参数
subprocess.call([path + '\\run.bat', path + '\\order.txt'], shell=true)
可以不用在python写入bat和txt文件后,再手动运行两者
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论