0

I am trying to run the below script with the four chunks referencing four other scripts; LIBERTY, TRA, GTS, and SOPI. The issue I have is a single script might run into an error but will clear once run again.

Is there a way to make a loop to run a max of 3 times for each chunk? For each chunk and not as a whole. If an error is received three times, for that chunk/outside script, it will provide an error.

The code,

import os

# Call on each scraper script to run

# LIBERTY
os.chdir("S:\Supply\Risk Management\Daily auto downloads\Storage\LIBERTY")
exec(open('LIBERTYScraper.py').read())

# TRA
os.chdir("S:\Supply\Risk Management\Daily auto downloads\Storage\TRA")
exec(open('TRAScraper.py').read())

# GTS
os.chdir("S:\Supply\Risk Management\Daily auto downloads\Storage\GTS")
exec(open('GSIScraper.py').read())

# SOPI 
os.chdir("S:\Supply\Risk Management\Daily auto downloads\Storage\SOPI")
exec(open('SOPIScraper.py').read())

# Finish statement
print("Scraper data pulls have been completed.")
2
  • So you want to run each chunk of code over if there is an error? Commented Jan 28, 2022 at 13:01
  • That is correct, if an error, run another time. If an error is received 3 times, then give the error and stop the script. Commented Jan 28, 2022 at 13:05

2 Answers 2

2

Something like this should work:

import os

# Call on each scraper script to run

# LIBERTY
os.chdir("S:\Supply\Risk Management\Daily auto downloads\Storage\LIBERTY")
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.")
Sign up to request clarification or add additional context in comments.

Comments

1

Something like this :

for i in range(3):
  try:
    os.chdir("S:\Supply\Risk Management\Daily auto downloads\Storage\LIBERTY")
    exec(open('LIBERTYScraper.py').read())
    break
  except:
    print("Error opening file")

2 Comments

Then just add "continue;" below the except part for it to follow through with the other code? Apologies, still fairly new to coding.
Added the break as we don't want it to iterate more if the file is opened successfully. continue is not essential.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.