[GIS] Select By Attribute and Export with Arcpy

arcpy

I have 4 folders with shapefiles in them, and for each folder I am selecting by attribute and then exporting the new shapefile. My code runs with no error but the output folders have no files in them and I am not sure if the problem is the way I am selecting by attribute or the way I am exporting. The code I am using is this:

import arcpy, os
arcpy.CheckOutExtension('Spatial')
arcpy.env.overwriteOutput = True
arcpy.env.overwriteOutput = 1
arcpy.env.qualifiedFieldNames = False

folders=r'E:\Sheyenne\Landsat_8\spatial'
out=r'E:\Sheyenne\Landsat_8\just_field_cells'
basename=os.listdir(folders)
for i in basename:
    if not os.path.isdir(os.path.join(out,i)):
            os.mkdir(os.path.join(out,i))
for folder in os.listdir(folders):
    arcpy.env.workspace=os.path.join(folders,folder)
    shapefiles=arcpy.ListFeatureClasses('*.shp')
    for shape in shapefiles:
        name=shape[:-4]
        arcpy.MakeFeatureLayer_management(shape, "lyr")
        arcpy.SelectLayerByAttribute_management ("lyr", "NEW_Selection", "FID_ = 233" or "FID_ = 604" or 
        "FID_ = 887" or "FID_ = 959" or "FID_ = 1686" or "FID_ = 1731" or "FID_ = 1753" or "FID_ = 1798"
        or "FID_ = 2058")
        arcpy.FeatureClassToFeatureClass_conversion("lyr", out, name)
print "Done"

Best Answer

I'd say that it's exporting but to the same directory and then overwriting or tripping up on the first iteration of listdir.. best to do all of it in one pass of listdir:

import arcpy, os
arcpy.CheckOutExtension('Spatial')
arcpy.env.overwriteOutput = True
arcpy.env.qualifiedFieldNames = False

folders=r'E:\Sheyenne\Landsat_8\spatial'
out=r'E:\Sheyenne\Landsat_8\just_field_cells'

for folder in os.listdir(folders):
    if os.path.isdir(os.path.join(folders,folder)): # only folders (directories) in folders
        outDir = os.path.join(out,folder)             # keep track of where it's going
        os.makedirs(outDir)                           # doesn't matter if it does or doesn't exist, make sure it's there

        arcpy.env.workspace=os.path.join(folders,folder)
        arcpy.AddMessage("Workspace is {0}, exporting to {1}".format(arcpy.env.workspace,outDir)) # follow the changing workspace
        shapefiles=arcpy.ListFeatureClasses('*.shp')
        for shape in shapefiles:
            name=shape[:-4]
            arcpy.AddMessage("Shapefile {0}".format(shape))
            arcpy.MakeFeatureLayer_management(shape, "lyr")
            arcpy.SelectLayerByAttribute_management ("lyr", "NEW_Selection", "FID_ in (233,604,887,959,1686,1731,1753,1798,2058)")
            arcpy.FeatureClassToFeatureClass_conversion("lyr", outDir, name)

When you use os.listdir() it will return all files and folders in the given path, your use of os.path.isdir() is incorrect and should be done like:

if os.path.exists(path):
    if os.path.isdir(path):

First for checking if something exists and then finding out if the existing object is a folder, not for checking if a folder (path) exists.

Related Question