I work with arcmap 10.3 and python 2.7.8. I have more than 500 shapefiles located in many folders and subFolders. All Sub Folders are located in one large directory. I try with arcpy to detect all shapefiles that have in their attribute table ,in field name "YEUD", the value 20. I search all shape files that begin with letters "mig". Finally i tried to print all the shapefiles that had been found with value 20 in it.
When i run this code i get en error:
import arcpy,os,sys,string,fnmatch import arcpy.mapping from arcpy import env rootPath = r"C:\Project\layers" pattern = 'mig*.shp' fields = arcpy.ListFields(rootPath) for root, dirs, files in os.walk(rootPath): for filename in fnmatch.filter(files, pattern): print( os.path.join(root, filename)) for field in fields: with arcpy.da.SearchCursor(rootPath, field.name) as rows: for row in rows: if field.name == 'YEUD': if row == '20': print( os.path.join(root, filename))
RuntimeError: ERROR 999999: Error executing function. >>>
I can't understand why it doesn't work ?
Solved! Go to Solution.
Here's a revised script for you.
import os
import fnmatch
import arcpy
rootPath = r"C:\Project\layers" 
pattern = 'mig*.shp' 
for root, dirs, files in os.walk(rootPath): 
    for filename in fnmatch.filter(files, pattern): 
        shp = os.path.join(root, filename)
        if arcpy.ListFields(shp, "YEUD"):
            print("{} has YEUD field".format(shp)) 
            with arcpy.da.SearchCursor(shp, ["YEUD"]) as rows:
                for row in rows:
                    if row[0] == 20:
                        print("{} has a record with YEUD = 20".format(shp))
                        breakA few notes:
Here's a revised script for you.
import os
import fnmatch
import arcpy
rootPath = r"C:\Project\layers" 
pattern = 'mig*.shp' 
for root, dirs, files in os.walk(rootPath): 
    for filename in fnmatch.filter(files, pattern): 
        shp = os.path.join(root, filename)
        if arcpy.ListFields(shp, "YEUD"):
            print("{} has YEUD field".format(shp)) 
            with arcpy.da.SearchCursor(shp, ["YEUD"]) as rows:
                for row in rows:
                    if row[0] == 20:
                        print("{} has a record with YEUD = 20".format(shp))
                        breakA few notes: