0

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?

Andy
1,3749 silver badges16 bronze badges
asked Nov 5, 2018 at 15:40
4
  • Put your try/except inside the for loop. As written your script will move past this section the first time an error occurs. Commented Nov 5, 2018 at 16:10
  • Did you mean to indent your print statement? Commented Nov 5, 2018 at 16:10
  • You want the try to be at the relevant level. Based on what you've written here, that would be right after for table in tableList:. Out of curiosity, how is tableList being populated? Commented Nov 5, 2018 at 16:11
  • tableList = [os.path.join(table_workspace, table) for table in arcpy.ListTables("table1w")] Commented Nov 6, 2018 at 6:59

2 Answers 2

1

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.

answered Nov 5, 2018 at 16:14
5
  • 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? Commented Nov 6, 2018 at 6:50
  • Also it gives the same error when it reaches to the missing dataset in the loop. Commented 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. Commented 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? Commented 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. Commented Nov 7, 2018 at 9:29
0

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)
answered Nov 6, 2018 at 0:20
1
  • 1
    But he said he wants to keep processing the tables that do exist. This will end the loop. Commented Nov 6, 2018 at 1:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.