凌的博客

您现在的位置是: 首页 > 学无止境 > python > 

python

python3 自动执行windows命令行cmd

2020-02-04 python 1499
import subprocess
import time
# import optparse

# parse = optparse.OptionParser(usage='"usage:%prog [options] arg1,arg2"', version="%prog 5.0")
# parse.add_option('-f', '--file', dest='file', action='store', type=str, metavar='file', help='请输入配置文件地址')
# options, args = parse.parse_args()

# cmd = ["test.py","-f","info2  \n"]
# subprocess.call(cmd,shell=True)
# time.sleep(1)
# subprocess.call("test.py -f infotest ",shell=True)


file = input("请输入配置文件(如:auto.txt):")
file = file if file!="" else "auto.txt" # 默认 auto.txt

with open(file,"r",encoding="utf-8") as f:
    cmd_content = f.read()

for cmd in cmd_content.split("\n"):
    cmd_shell = cmd.split(",")
    print("cmd run:%s" % " ".join(cmd_shell))
    subprocess.call(cmd_shell, shell=True)
    print(".1秒后执行下一个任务")
    time.sleep(.1)


input("auto run success")


文章评论

0条评论