arcpy.ListFields() returning "does not exist"

1638
6
Jump to solution
09-07-2012 04:24 AM
KevinYanuk
Occasional Contributor
I'm looking to compare a text file of features classes and their fields to those in a map doc.

I'm using:

for de in searchfile.readlines():     if '--' in de:         fc=de.split('--')         for lyr in arcpy.mapping.ListLayers(mxd):             if lyr.name==fc[1].split('\n')[0]:                 print lyr.name,type(lyr)                 for sc in arcpy.ListFields(lyr):                     if not sc in searchfile.readlines():                         print sc


where the first few lines are just string parsing the text file to locate the featureclass names.  I can get to

if lyr.name==fc[1].split('\n')[0]:     print lyr.name,type(lyr)


which prints the names of the layers fine, along with the type "<class 'arcpy._mapping.Layer'>"

edit: However, it returns the following when I try to ListFields:


Traceback (most recent call last):   File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 323, in RunScript     debugger.run(codeObject, __main__.__dict__, start_stepping=0)   File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\debugger\__init__.py", line 60, in run     _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)   File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\debugger\debugger.py", line 655, in run     exec cmd in globals, locals   File "C:\Users\kyanuk\Desktop\changesearch.py", line 16, in <module>     for sc in arcpy.ListFields(lyr):   File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\__init__.py", line 787, in ListFields     return gp.listFields(*args)   File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 342, in listFields     self._gp.ListFields(*gp_fixargs(args))) IOError: "SIDEWALK" does not exist



Can ListFields() be used this way for these featureclasses in my map doc?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
I think I may have figured out what is wrong.  Since the ListFields function is not part of the 'mapping' module, it needs to read the full path of the layer. 

Replace:

for sc in arcpy.ListFields(lyr):


with:

for sc in arcpy.ListFields(lyr.dataSource):


Entire code:


for de in searchfile.readlines():     if '--' in de:         fc=de.split('--')         for lyr in arcpy.mapping.ListLayers(mxd):             if lyr.name==fc[1].split('\n')[0]:                 print lyr.name,type(lyr)                 for sc in arcpy.ListFields(lyr.dataSource):                     if not sc.name in searchfile.readlines():                         print sc.name

View solution in original post

0 Kudos
6 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Kevin,

"for sc in arcpy.ListFields(lyr)"  will return a field object.  Try specifying the 'name' property for this object.  Ex:

for de in searchfile.readlines():
    if '--' in de:
        fc=de.split('--')
        for lyr in arcpy.mapping.ListLayers(mxd):
            if lyr.name==fc[1].split('\n')[0]:
                print lyr.name,type(lyr)
                for sc in arcpy.ListFields(lyr):
                    if not sc.name in searchfile.readlines():
                        print sc.name
0 Kudos
KevinYanuk
Occasional Contributor
Hi Kevin,

"for sc in arcpy.ListFields(lyr)"  will return a field object.  Try specifying the 'name' property for this object.  Ex:

for de in searchfile.readlines():
    if '--' in de:
        fc=de.split('--')
        for lyr in arcpy.mapping.ListLayers(mxd):
            if lyr.name==fc[1].split('\n')[0]:
                print lyr.name,type(lyr)
                for sc in arcpy.ListFields(lyr):
                    if not sc.name in searchfile.readlines():
                        print sc.name



Thanks for the reply - unfortunately, it is still returning the same error ... "[layer]" Does not exist, even though it is printing the layer name at the
print lyr.name,type(lyr)
line...
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Can you attach a sample of your text file?
0 Kudos
KevinYanuk
Occasional Contributor
Can you attach a sample of your text file?


It is basically laid out where anything with a '--' in front is the feature class, and what is below are the fields they should contain.


--FEATURECLASS1
FIELD
FIELD
FIELD
FIELD
--FEATURECLASS2
FIELD
FIELD
FIELD
FIELD
FIELD
--FEATURECLASS3
FIELD
FIELD
FIELD
FIELD
0 Kudos
JakeSkinner
Esri Esteemed Contributor
I think I may have figured out what is wrong.  Since the ListFields function is not part of the 'mapping' module, it needs to read the full path of the layer. 

Replace:

for sc in arcpy.ListFields(lyr):


with:

for sc in arcpy.ListFields(lyr.dataSource):


Entire code:


for de in searchfile.readlines():     if '--' in de:         fc=de.split('--')         for lyr in arcpy.mapping.ListLayers(mxd):             if lyr.name==fc[1].split('\n')[0]:                 print lyr.name,type(lyr)                 for sc in arcpy.ListFields(lyr.dataSource):                     if not sc.name in searchfile.readlines():                         print sc.name
0 Kudos
KevinYanuk
Occasional Contributor
I think I may have figured out what is wrong.  Since the ListFields function is not part of the 'mapping' module, it needs to read the full path of the layer. 

Replace:

for sc in arcpy.ListFields(lyr):


with:

for sc in arcpy.ListFields(lyr.dataSource):


Entire code:


for de in searchfile.readlines():
    if '--' in de:
        fc=de.split('--')
        for lyr in arcpy.mapping.ListLayers(mxd):
            if lyr.name==fc[1].split('\n')[0]:
                print lyr.name,type(lyr)
                for sc in arcpy.ListFields(lyr.dataSource):
                    if not sc.name in searchfile.readlines():
                        print sc.name



Ah!  Perfect, worked like a charm.  Thank you for your help Jake, much appreciated.
0 Kudos