I am trying to read a CSV then iterate through an sde to find matching features, their fields, and then print them. There is a table in the list and I'm not able to skip over it and continue reading the CSV. I get the "IOError: table 1 does not exist" and I only get the features that come before the table.
import arcpy
from arcpy import env
import sys
import os
import csv
with open('C:/Users/user/Desktop/features_to_look_for.csv', 'r') as t1:
objectsinESRI = [r[0] for r in csv.reader(t1)]
env.workspace = "//conn/[email protected]"
fcs = arcpy.ListFeatureClasses('sometext.*')
for fcs in objectsinESRI:
fieldList = arcpy.ListFields(fcs)
for field in fieldList:
print fcs + " " + ("{0}".format(field.name))
Sample CSV rows (can't seem to post a screenshot of the excel file)
feature 1
feature 2
feature 3
feature 4
table 1
feature 5
feature 6
feature 7
feature 8
feature 9
Result
feature 1
feature 2
feature 3
feature 4
Desired Result
feature 1
feature 2
feature 3
feature 4
feature 5
feature 6
feature 7
feature 8
feature 9
2 Answers 2
Add a try\except
to skip the feature classes/tables that don't exist.
for fcs in objectsinESRI:
try: fieldList = arcpy.ListFields(fcs)
except IOError: continue
for field in fieldList:
print fcs + " " + ("{0}".format(field.name))
-
This worked. I tried this route already and I'm not sure what I did but couldn't get it to work. Thanks, this is what I was looking for.ajd018– ajd0182019年03月14日 12:42:50 +00:00Commented Mar 14, 2019 at 12:42
Try adding logic to omit any instance of "Table" from your list comprehension. Also, note that Search Cursors can read csv files directly, so no need to open the file with the csv
package. For example:
import arcpy
csv = r'C:/Users/user/Desktop/features_to_look_for.csv'
rows = [row[0] for row in arcpy.da.SearchCursor(csv, "some_field") if "table" not in row[0]]
Alternatively, you can check for the existence of a file using Exists()
and perform an action only of that file exists using an if
statement. Then create a log of the files that do not exist. For example:
import arcpy, os
csv = r'X:\temp\my_csv.csv'
ws = r'conn\[email protected]'
does_not_exist = []
for row in [row[0] for row in arcpy.da.SearchCursor(csv, "some_field")]:
path = os.path.join(ws, row)
if arcpy.Exists(path):
fields = [field.name for field in arcpy.ListFields(path)]
print path + " " + ("{0}".format(fields))
else:
does_not_exist.append(path)
print "The following files do not exist: {0}".format(does_not_exist)
fcs
to something:fcs = arcpy.ListFeatureClasses('sometext.*')
and then stomp on that in thefor
loop? Whichfcs
is the real one in the loop? I don't think its the CSV reading at fault, all the lines are read intoobjectsinESRI
correctly - print it and check.