当前位置: 代码网 > it编程>前端脚本>Python > python非阻塞式后台如何运行bat脚本

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

2024年07月04日 Python 我要评论
python非阻塞式后台运行bat首先,bat脚本要实现后台运行,代码如下:c:\users\linuxbugs\desktop\demo\run_demo.bat@echo off if "%1"

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

总结

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com