1

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/features@dev.sde"
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
  • 1
    why do you set fcs to something: fcs = arcpy.ListFeatureClasses('sometext.*') and then stomp on that in the for loop? Which fcs is the real one in the loop? I don't think its the CSV reading at fault, all the lines are read into objectsinESRI correctly - print it and check. – Spacedman Mar 13 '19 at 18:46
  • Cross-posted as stackoverflow.com/q/55146907/820534 – PolyGeo Mar 14 '19 at 13:33
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 Mar 14 '19 at 12:42
0

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\features@dev.sde'

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)

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.