TransWikia.com

Add Field doesn't work on ArcPy (ArcGIS) in a loop

Geographic Information Systems Asked by skall411 on October 4, 2021

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:UsersDropboxTrade_DiasporaLCPLCP_gvtyCntrsaddfieldonly.py", line 16, in <module>
    arcpy.AddField_management(absFile,"cityid","SHORT")
  File "C:Program Files (x86)ArcGISDesktop10.6ArcPyarcpymanagement.py", line 3424, in AddField
    raise e
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (AddField).

One Answer

Apparently the issue is with the Dropbox. Working on the Dropbox folder sometimes can create file locking issues that lead to random failures and data corruption for GIS data. I moved my files to another location and it worked perfectly.

Answered by skall411 on October 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP