I have self taught Python coding, often with much help from StackOverflow (Thanks for all your help in the past!). I have Python 3 code triggered via a Windows .bat file which writes output data to a Windows mounted network drive. This mount has a slow connection with intermittent and brief connection drops. I'm trying to come up with Python 3 code that will catch the permission error I receive during the network drops and then delay for 3 seconds. I then want the code to try again up to 3 more times before gracefully erroring out. The current code I inherited simply crashes out and the entire code execution must be started over.
Here are my code adaptations so far, but I'm struggling with how to craft the try again until successful, or abort the code execution after 3 failures.
def log(log_file, txt):
"""Writes input text to the log file."""
if os.path.isfile(log_file):
## Try to catch the permission error and avoid a crash due to network mount drive delays
## and insert a 3 second pause before trying again.
try:
with open(log_file, "a") as out_file:
out_file.write(txt)
except PermissionError as error:
## Print needing to pause then try again message to user
print(f'WARNING: Possible network problem. Trying again in 3 seconds.')
time.sleep(3)
## Insert code to force try again
else:
## Log file missing; send message to user
print(f'ERROR: log function could not find {log_file}')
return