天天看點

python調用cmd執行指令_python3 自動執行windows指令行cmd

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")

如果本文對您有所幫助,請支援下本站哦!!!^_^