Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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") 
share|improve this question
Welcome to gis.SE. You look like you're using arcpy in here - can you add details of which version to the question (just click edit below the question)? Also, can you fix the formatting on your code so the whitespace matches what you are really using? – BradHards Mar 1 '14 at 22:28
It looks as if the one loop method only has ".CA.shp"'s and no ".AZ.shp"'s returned from ListFiles. – gm70560 Mar 2 '14 at 0:18
Yes, that is what I am having with when I make one loop. CA.shp will work but when it gets to the AZ part is said error executing. I notices that if I replace AZshp, from the AddField and CalculateField, with CA the whole loop will work, but that would mean my stateAbbrev for AZ would be CA and not AZ. This is why I made 2 loops because it worked that, but I want to see if it is possible to make 1 loop. – Trey Mar 2 '14 at 0:33
You seem to have had some advice and provided more details as Comments, but do not yet seem ready to Accept an Answer, so I recommend that you revise your Question using its edit button to try and make it clearer. – PolyGeo Mar 2 '14 at 11:25

Try extending the list of files; the elif isn't finding any AZ's since [CAshp] only has CA's from "*CA.shp".

CAshp = arcpy.ListFiles("*_CA.shp")
for CA in CAshp: 
    if CA == 'roads_CA.shp' or CA == 'railroads_CA.shp':
        arcpy.AddField_management(CA, 'StateAbbre', 'TEXT')
        arcpy.CalculateField_management(CA, newField, "'CA'", "PYTHON_9.3")
    elif CA == 'roads_AZ.shp' or CA == 'railroads_AZ.shp':
        arcpy.AddField_management(CA, 'StateAbbre', 'TEXT')
        arcpy.CalculateField_management(AZshp, newField, "'AZ'", "PYTHON_9.3")

for clarity, maybe I should have renamed the varibles:

# add CA's and then extend list with AZ's
shapes = arcpy.ListFiles("*_CA.shp")
for shp in shapes: 
    # first part of the loop for CA's
    if shp == 'roads_CA.shp' or shp == 'railroads_CA.shp':
        arcpy.AddField_management(shp, 'StateAbbre', 'TEXT')
        arcpy.CalculateField_management(shp, newField, "'CA'", "PYTHON_9.3")
    # second part of the loop for AZ's
    elif shp == 'roads_AZ.shp' or shp == 'railroads_AZ.shp':
        arcpy.AddField_management(shp, 'StateAbbre', 'TEXT')
        arcpy.CalculateField_management(shp, newField, "'AZ'", "PYTHON_9.3")
share|improve this answer
I ran your code and it came out an error: arcpy.CalculateField_management(AZshp, newField, "'AZ'", "PYTHON_9.3") NameError: name 'AZshp' is not defined – Trey Mar 2 '14 at 6:14
I then gave it a name AZshp = CAshp.extend(arcpy>ListFiles("*_AZ.shp") but I still got the same error. – Trey Mar 2 '14 at 6:19
But if I change AZshp with CA, it will create a new field and add 'AZ' to the *_CA.shp shapefile. For the elif, I want it to create a new field containing AZ for *AZ.shp. – Trey Mar 2 '14 at 17:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.