2

I have 100 shapefiles that all have 2 fields in common called Type2017 and Type2018. The whole of the fields have only one number (1 or 2 or 3). fo example, Type2017 = 1 or Type2018 = 2

enter image description here

I would like to copy those shapefiles which one of these fields have been filled with number 1.

Usually I use this code for copying shapefiles to another folder:

NewFolder = WORK_DIRECTORY + r'/01_SHP'
copyFeatureClass = os.path.join(NewFolder, os.path.basename(Shapefile))
arcpy.CopyFeatures_management(Shapefile, CopyFeatureClass)

But I don’t know how to input the field value that I want .

  • 1
    You would like to copy the shapefiles where either the Type2017 or Type2018 field is completely populated with 1 or has at least one record populated with 1? – reevesii Nov 7 '18 at 14:07
  • Only copying the shapefile in different location. Because I don’t know out of 100 shapefile which one has fields that filed with number 1. – Anahita Kp Nov 7 '18 at 14:13
2

I am assuming you have created the list of shapefiles. You can read the attributes with the searchcursor and copy if 1 is found (only checking the first row):

for shape in shapelist:
    with arcpy.da.SearchCursor(shape,["Type2017","Type2018"]) as cursor:
        for row in cursor:
            if 1 in row:
                #Add copy code here
            break
-2
arcpy.env.workspace = r'PATH_TO_FOLDER'

list_fc = arcpy.ListFeatureClasses()
out_path =r'OUTPATH'
for fc in list_fc:
    arcpy.MakeFeatureLayer_management(fc, "temp_fc")
    arcpy.SelectLayerByAttribute_management("temp_fc", "NEW_SELECTION", 'Type2017 = 1 OR Type2018 = 1')
    Num = str(arcpy.GetCount_management("temp_fc")) 
    if Num != "0":
        arcpy.CopyFeatures_management("temp_fc", out_path +"/" + fc)
    else:
         print('Deleted empty shapefiles')
  • Dear @BERA, fixed my mistake. It was working, when there was no selection empty shps were returned. Now it checks for that and only when selection occurs shapes are created. – JuniorPythonNewbie Nov 8 '18 at 7:59

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.