我正在尝试运行下面的脚本,其中四个块引用了其他四个脚本;LIBERTY、TRA、GTS和SOPI。我遇到的问题是,单个脚本可能会遇到错误,但一旦再次运行就会清除。
有没有一种方法可以让每个区块循环最多运行3次?对于每个区块而不是作为一个整体。如果收到三次错误,那么对于该块/外部脚本,它将提供一个错误。
代码,
import os
# Call on each scraper script to run
# LIBERTY
os.chdir("S:SupplyRisk ManagementDaily auto downloadsStorageLIBERTY")
exec(open('LIBERTYScraper.py').read())
# TRA
os.chdir("S:SupplyRisk ManagementDaily auto downloadsStorageTRA")
exec(open('TRAScraper.py').read())
# GTS
os.chdir("S:SupplyRisk ManagementDaily auto downloadsStorageGTS")
exec(open('GSIScraper.py').read())
# SOPI
os.chdir("S:SupplyRisk ManagementDaily auto downloadsStorageSOPI")
exec(open('SOPIScraper.py').read())
# Finish statement
print("Scraper data pulls have been completed.")
这样的东西应该可以工作:
import os
# Call on each scraper script to run
# LIBERTY
os.chdir("S:SupplyRisk ManagementDaily auto downloadsStorageLIBERTY")
with open('LIBERTYScraper.py') as f:
for _ in range(3):
try:
exec(f.read())
break;
except Exception: # generaly not a great idea, put a more refined exception
continue;
# Finish statement
print("Scraper data pulls have been completed.")
类似这样的东西:
for i in range(3):
try:
os.chdir("S:SupplyRisk ManagementDaily auto downloadsStorageLIBERTY")
exec(open('LIBERTYScraper.py').read())
break
except:
print("Error opening file")