I am trying to add a field to each shapefile inside several folders in ArcGIS with Arcpy. I am executing the following code.
import arcpy, os
# Overwrite pre-existing files
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")
regions = ["China","India","Euro","ArabPersia"]
years = ["622","800","900","1000","1100","1200","1300","1400","1500","1600"]
for region in regions:
for year in years:
folder = r'C:/Users/Dropbox/Trade_Diaspora/LCP/LCP_gvtyCntrs'+"//"+year+"//"+region+"//"+'polylines'
for root, dirs, files in os.walk(folder):
for name in files:
if name.endswith('.shp'):
shpName = os.path.splitext(name)[0]
absFile = os.path.abspath(os.path.join(root,name))
arcpy.DeleteField_management(absFile,"cityid")
arcpy.AddField_management(absFile,"cityid","SHORT")
arcpy.CalculateField_management(absFile, "cityid",shpName,"VB")
print("complete " + region + " " + year + " "+ shpName + " field addition")
print("Completed All "+region+ " " +year)
The problem is that it works sometimes for some random times. In some cases, it loops through like 50-60 files before throwing an error. Sometimes it throws an error after 2 loops. What I have tried:
- Tried to run for a single folder (No first two loops)
- Tried with adding empty values to options in the AddField command.
- Tried to run both on ArcMap's python as well as a stand-alone script.
- Started without DeleteField command for files that do not have the field for sure.
- Tried with just AddField not the CalculateField command.
- Tried with adding another field (also changed names)
- Switched my internet connection to see if there is any issue (ArcGIS needs to run on VPN)
None of these worked in my case. I am using ArcGIS 10.6, Python 2.7.14, 32 bit (can't use 64 bit because I need to convert these into Excels later), Windows 10, with 32GB RAM.
Every time, I get the same error in the AddField command:
Traceback (most recent call last):
File "C:\Users\Dropbox\Trade_Diaspora\LCP\LCP_gvtyCntrs\addfieldonly.py", line 16, in <module>
arcpy.AddField_management(absFile,"cityid","SHORT")
File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\management.py", line 3424, in AddField
raise e
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (AddField).