python非阻塞式后台如何运行bat脚本

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文件后,再手动运行两者

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容