AnsweredAssumed Answered

Update Cursor for a list of shp

Question asked by avillafuerte on Nov 4, 2018
Latest reply on Nov 5, 2018 by bixb0012

Hello,

I want to add the data source (path name) to each attribute table . All of them were compiled from many folders so it's quite important for us to keep track. I'm quite new to python and I'm having trouble applying the update cursor function to populate the path name into the attribute table of each layer. This is what I have so far and I'm getting the error of 'in_table' is not a table or a featureclass. So I understand the list named 'FeatureClasses' is not a valid input for the update cursor function. How can I fix the code so it iterates through each shp listed in my mxd?

mxd= arcpy.mapping.MapDocument (r"path.mxd") 

#List with all the shp in the mxd
FeatureClasses = arcpy.mapping.ListLayers (mxd)

# Add a field to each shp
for fc in FeatureClasses():
    arcpy.AddField_management(fc, "Path", "TEXT", field_length = 250)

#Update attribute table with their pathnames
for fc in FeatureClasses:
    # pull out the shapefile name
    shpName = layer.dataSource
    # define update cursor
    with arcpy.da.UpdateCursor(FeatureClasses, ("Path")) as cursor:
        for row in cursor:
            # set Path to the shapefile name for each row
            row[0] = shpName
            cursor.updateRow(row)

I also tried this other code that I found online and shows no errors but the field 'Path' it's still blank. When I print the 'Test' list it's also blank.

Test = arcpy.ListFeatureClasses("*.shp")
print Test
[]

Test = arcpy.ListFeatureClasses("*.shp")
for calc in Test:
     # pull out the shapefile name
     shpName = os.path.abspath
     # define update cursor
     with arcpy.da.UpdateCursor(calc, ("Path")) as cursor:
         for row in cursor:
             # set 'Path' to the shapefile name for each row
             row[0] = shpName
             cursor.updateRow(row)

Thanks in advance!

Outcomes