问题遇到的现象和发生背景
python调用cmd顺序执行命令失败
代码
from time import time
from openpyxl import load_workbook
from tenacity import retry
import subprocess
from tenacity import retry,stop_after_attempt
import subprocess
global i
i=0
def replace(obj):
if obj is None:
obj = ''
return obj
wb = load_workbook("./csv/information.xlsx")
'''
excel.xlsx格式
Bug Date De
41 2022-06 g:9
41 2022-09 g:6
'''
ws = wb['Sheet1']
contexts = []
for row in range(2, ws.max_row + 1):
bug_id = ws["A" + str(row)].value
date_id = ws["B" + str(row)].value
dev_id = ws["C" + str(row)].value
context=f".\Get \"{date_id}\" -devId \"{dev_id}\" -Path \"D:\\{bug_id}.txt\""
contexts.append(context)
print(contexts)
# for context in contexts:
# print(context)
# s = "&" .join (contexts)
# print(s)
@retry(stop=stop_after_attempt(2))
def runcmd():
ret = subprocess.run(contexts[i],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding="utf-8",cwd=r'C:\Users\Localadmin\Desktop\GetData\')
for i in contexts:
if ret.returncode!=0:
i=i+1
if ret.returncode != 0:
print("error:",ret)
print('失败重试中')
raise Exception
else:
print("success:",ret)
runcmd('%s'%contexts[i])#字符串参数
运行结果
如果执行命令失败重试2次之后就直接报错,不会执行下一条命令
我想要达到的结果
执行命令失败后重试两次,如果还失败就执行下一条,执行成功就直接执行下一条,直到执行完所有命令