I have this code:
... for table in tableList:
... with arcpy.da.SearchCursor(table, ["qname","tname","query"]) as cur:
... for row in cur:
... if row[2] not in ('',None):
... query = row[2]
... arcpy.Select_analysis(in_features=row[1],
... out_feature_class=os.path.join(shapefile_output_folder,row[0]),where_clause=query)
If a certain file that meets at the loop doesn't exist I want to print the name of the file and continue without stopping.
The error when it doesn't find the file is :
Runtime error Traceback (most recent call last): File "", line 8, in File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\analysis.py", line 90, in Select raise e ExecuteError: ERROR 000732: Input Features: Dataset TA2a does not exist or is not supported
So how should the exception be? I tried:
try
... for table in tableList:
... with arcpy.da.SearchCursor(table, ["qname","tname","query"]) as cur:
... for row in cur:
... if row[2] not in ('',None):
... query = row[2]
... arcpy.Select_analysis(in_features=row[1],
... out_feature_class=os.path.join(shapefile_output_folder,row[0]),where_clause=query
)
except arcpy.RuntimeError:
print(table)
Can you suggest corrections?
2 Answers 2
You don't need a try statement. You can specifically test whether the table exists prior to starting the cursor:
for table in tableList:
if not arcpy.Exists(table):
print('{} does not exist'.format(table))
continue
with arcpy.da.SearchCursor(table, ["qname","tname","query"]) as cur:
for row in cur:
if row[2] not in ('',None):
query = row[2]
arcpy.Select_analysis(in_features=row[1],
out_feature_class=os.path.join(shapefile_output_folder,row[0]),where_clause=query)
I find a conditional and continue to be cleaner than try/except.
-
It is better because we know the possible error i may be caused right. If we didn't know what error it could be caused we would do the try approach?user51332– user513322018年11月06日 06:50:07 +00:00Commented Nov 6, 2018 at 6:50
-
Also it gives the same error when it reaches to the missing dataset in the loop.user51332– user513322018年11月06日 07:23:25 +00:00Commented Nov 6, 2018 at 7:23
-
@user51332, You are correct that if there was some other error, it would raise an Exception, but if you said "If a certain file that meets at the loop doesn't exist..."; this addresses your specific request. Also, if there's another error, you probably want to see the Exception so that you can resolve it rather than printing a misleading message that the dataset doesn't exist and then continuing.Tom– Tom2018年11月06日 17:18:13 +00:00Commented Nov 6, 2018 at 17:18
-
@user51332, If you're getting the same error, then the issue isn't that the dataset doesn't exist, but that it can't be read by a cursor. I repeat my question from above: how is tableList being populated?Tom– Tom2018年11月06日 17:19:13 +00:00Commented Nov 6, 2018 at 17:19
-
I have already answered your question below the comment that you asked.In the comments of the question. The way i fixed it with try except was to include only the select analysis line. If you can make it your way to work would be great though.without try except.user51332– user513322018年11月07日 09:29:57 +00:00Commented Nov 7, 2018 at 9:29
You're close. It's actually arcpy.ExecuteError.
try:
... for table in tableList:
... with arcpy.da.SearchCursor(table, ["qname","tname","query"]) as cur:
... for row in cur:
... if row[2] not in ('',None):
... query = row[2]
... arcpy.Select_analysis(in_features=row[1],
... out_feature_class=os.path.join(shapefile_output_folder,row[0]),where_clause=query
)
except arcpy.ExecuteError:
print(table)
-
1But he said he wants to keep processing the tables that do exist. This will end the loop.Tom– Tom2018年11月06日 01:03:04 +00:00Commented Nov 6, 2018 at 1:03
for table in tableList:
. Out of curiosity, how is tableList being populated?tableList = [os.path.join(table_workspace, table) for table in arcpy.ListTables("table1w")]