I'm using the arcpy module of ArcGIS in Python (2.7) to process many polygon shapefiles, using many different tools. Every so often it will throw a random error, which I catch with an exception but all subsequent shapefiles are then affected by the same error. I really don't understand what is causing this error (ERROR 010088), and the only workaround I have is to restart the script from the last file that was processed successfully.
My question is: how can I restart the script every time I hit this error, and then stop when all files have been processed successfully?
I've looked at various different questions (e.g. Restarting a self-updating python script) but nothing quite does the job, or I can't understand how to apply it to my situation because I'm still very much a Python beginner. The closest I've come is the example below, based on this blog post: https://www.alexkras.com/how-to-restart-python-script-after-exception-and-run-it-forever/.
Script called test.py:
import arcpy
import sys
try:
arcpy.Buffer_analysis(r"E:\temp\boundary.shp",
r"E:\temp\boundary2.shp",
"100 Feet")
# Print arcpy execute error
except arcpy.ExecuteError as e:
# Print
print(e)
# Pass any other type of error
except:
pass
Script called forever.py, in the same directory:
from subprocess import Popen
import sys
filename = sys.argv[1]
while True:
print("\nStarting " + filename)
p = Popen("python " + filename, shell=True)
p.wait()
(Note that boundary.shp is just a random boundary polygon - available here: https://drive.google.com/open?id=1LylBm7ABQoSdxKng59rsT4zAQn4cxv7a).
I'm on a Windows machine, so I run all this in the command line with:
python.exe forever.py test.py
As expected, the first time this script runs without errors, and after that it hits an error because the output file already exists (ERROR 000725). The trouble is that ultimately I want the script to only restart when it hits ERROR 010088, and definitely not when the script has completed successfully. So in this example, it should not restart at all because the script should be successful the first time it is run. I know in advance how many files there are to process, so I know the script has finished successfully when it reaches the last one.