I am trying to use for loop to add and calculate a newfield for 2 shapefiles and a different new field for 2 other shapefiles. When I tried to do these 2 loops as one, it did not work so I separated them into 2 loops and it worked. I am using python 2.7 stand alone (arcmap 10.2).
more details: So I want to make a new field and add 'CA' to shapefiles containing _CA.shp (*_CA.shp). I also want to make a new field and add 'AZ' to shapefiles containing _AZ.shp (*_CA.shp). When I created a loop (the first code), I got my roads_CA and railroads_CA to work but roads_AZ and railroads_AZ did not create a new field containing 'AZ'. I did not get an error which means the loop ran through but I guess the elif came out false so it did not create anything.
here is my code:
CAshp = arcpy.ListFiles("*_CA.shp")
for CA in CAshp:
if (CAshp == 'roads_CA.shp', CAshp == 'railroads_CA.shp'):
newField = 'StateAbbre'
arcpy.AddField_management(CA, newField, 'TEXT')
arcpy.CalculateField_management(CA, newField, "'CA'", "PYTHON_9.3")
elif (CAshp != 'roads_AZ.shp', CAshp != 'railroads_AZ.shp'):
AZshp = CAshp.extend(arcpy.ListFiles("*_AZ.shp"))
newField = 'StateAbbre'
arcpy.AddField_management(AZshp, newField, 'TEXT')
arcpy.CalculateField_management(AZshp, newField, "'AZ'", "PYTHON_9.3")
Best Answer
Try extending the list of files; the elif isn't finding any AZ's since [CAshp] only has CA's from "*CA.shp".
for clarity, maybe I should have renamed the varibles: