利用python执行.bat文件

首先中d:/start.bat;详情如下:

start cmd   

编写python文件

#coding=utf-8
import subprocess

def cmd_test():
    # cmd = 'cmd.exe d:/start.bat'
    p = subprocess.Popen("cmd.exe /c" + "d:/start.bat", stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    curline = p.stdout.readline()
    while (curline != b''):
        print(curline)
        curline = p.stdout.readline()
    p.wait()
    print(p.returncode)

if __name__ == '__main__':
    cmd_test()

执行时可以打开cmd窗口;丰富.bat文件可以达到意想不到的效果,如:monkey命令的自动执行

参考:https://www.jianshu.com/p/430c411160f8

posted @ 2020-07-07 14:16  pathbreaker  阅读(18174)  评论(0编辑  收藏  举报