Close all ESRI applications. Then only start the one where you will do the editing (eg. Arcmap).
This will release all locks that are on your data from other applications, which are interfering with your data manipulation.
If it still doesn't work, also try to restart your computer.
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")
CAshp.extend(arcpy.ListFiles("*_AZ.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")
shapes.extend(arcpy.ListFiles("*_AZ.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")
Best Answer
From ArcGIS Rules for working with schema locks:
As Martin and PolyGeo noted in comments above, if the file is open in any other Esri software -- ArcCatalog, another instance of ArcMap, or Python -- that is causing the schema lock. If shutting down all ArcGIS software and restarting ArcMap doesn't release it, then you may need to reboot the computer.
A schema lock can also occur if your data is on a network or in an SDE database, althouhg yours appears to be on the C: drive so that is probably not the case here. If that does occur, however, then you need to contact other users who may be viewing the data and ask them to stop.