VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • python非阻塞式后台如何运行bat脚本

这篇文章主要介绍了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文件后,再手动运行两者

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

原文链接:https://blog.csdn.net/Woodrow1994/article/details/114649528


相关教程