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?

4
  • Put your try/except inside the for loop. As written your script will move past this section the first time an error occurs.
    – Bjorn
    Nov 5 '18 at 16:10
  • Did you mean to indent your print statement?
    – Bjorn
    Nov 5 '18 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?
    – Tom
    Nov 5 '18 at 16:11
  • tableList = [os.path.join(table_workspace, table) for table in arcpy.ListTables("table1w")]
    – user51332
    Nov 6 '18 at 6:59
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.

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?
    – user51332
    Nov 6 '18 at 6:50
  • Also it gives the same error when it reaches to the missing dataset in the loop.
    – user51332
    Nov 6 '18 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
    Nov 6 '18 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
    Nov 6 '18 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
    Nov 7 '18 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)
1
  • 1
    But he said he wants to keep processing the tables that do exist. This will end the loop.
    – Tom
    Nov 6 '18 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.