1

I know a little Python/arcpy, enough to barely get me by. My goal was to, on a regular task schedule, convert feature classes to shapefiles and overwrite existing shapefiles, but I realized that arcpy.overwriteoutput = True did not work. So, as a work around, I added an os remove code to remove all files ending in .shp, .dbf. prj etc. I ran it in ArcCatalog and it was successful in deleting some file types but not all of the file types associated with the shapefile, and although I commanded it to delete .shx, .cbg, and .sbn files, they still remained in the folder. I also tried arcpy.CopyFeatures_management to copy the feature classes to the existing shapefiles in the workspace, but I was unsuccessful in even getting a version of a script to work.

Here is an example of a code that works but runs in an infinite loop.

(I replaced the workspaces, folders, and destinationoutput) Please focus on the infinite for loop.

 import os
 import arcpy
 dir_name = C:\Users\Employee\Documents\ArcGIS
 location = os.listdir(dir_name)
 extensions = (".shp", ".prj", ".dbf", ".xml", ".cpg", ".sbn", ".sbx" ".shx")
 for item in location:
     if item.endswith(extensions):
         os.remove(os.path.join(dir_name, item))
 arcpy.overwriteoutput = True
 arcpy.env.workspace = C:\Users\Employee\Documents\ArcGIS\NSD_exampleschemaCOPY.gdb
 destinationOutput = C:\Users\Employee\Documents\ArcGIS\HansenTest
 datasetList = arcpy.ListDatasets()
 waterfcList = arcpy.ListFeatureClasses("", "", "WaterDistribution")
 for datasets in datasetList:
     for fc in waterfcList:
         if fc in waterfcList:
             arcpy.FeatureClassToShapefile_conversion(fc, destinationOutput)
         elif fc == "MST_Temp_Pipeline_Project":
             break
         else:
             arcpy.AddMessage("Complete")

I also tried this to work but it keeps processing with no end in sight:

import arcpy
arcpy.env.overwriteOutput=True
arcpy.env.workspace = r'C:\Users\Employee\Documents\afgdb.gdb'
outWorkspace = r'C:\Users\Employee\Documents\Employee'
datasetLists = arcpy.ListDatasets()
fcList = arcpy.ListFeatureClasses()
cases = ("watertest", "watertest1", "watertest2", "sewertest", "sewertest1", "sewertest2", "sewertest3")
for datasets in datasetLists:
    for shapefile in fcList:
        for shapefile in cases: 
            outFeatureClass = os.path.join(outWorkspace, shapefile.strip(".gdb"))
            arcpy.CopyFeatures_management(shapefile, outFeatureClass)

I for some reason couldn't answer my own question, but the script below works without looping infinitely.

import arcpy
arcpy.env.overwriteOutput=True
arcpy.env.workspace = r'C:\Users\bnamsaly\Documents\New File Geodatabase.gdb'
outWorkspace = r'C:\Users\bnamsaly\Documents\Vicky'
datasetLists = arcpy.ListDatasets()
fcList = arcpy.ListFeatureClasses()
cases = ("watertest", "watertest1", "watertest2", "sewertest", "sewertest1", "sewertest2", "sewertest3")
for shapefile in cases:
    try:
        outFeatureClass = os.path.join(outWorkspace, shapefile.strip(".gdb"))
        arcpy.CopyFeatures_management(shapefile, outFeatureClass)
    except arcpy.ExecuteError:
        tb = sys.exc_info()[2] # execute errors from Python
        tbinfo = traceback.format_tb(tb)[0]
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
        arcpy.AddError(pymsg)
        msgs = "ARCGIS ERRORS:\n" + arcpy.GetMessages(2) + "\n"
        arcpy.AddError(msgs)
  • 2
    It is called arcpy.env.overwriteOutput=True (.env and big O). Do you have the shapefiles opened which could cause them to be locked? – BERA Jan 26 at 7:27
  • Thanks for that, I will try a version of the script using the correct syntax. Also I don't think that will help me with my infinite loop and no, the shapefiles were not open. I'm shifting my script to delete and do a featureclasstoshapefile conversion instead. I don't imagine it would be possible to do a copyfeaturemanagement function to multiple feature classes/shapefiles even if I call them out in a list. – Vicky Nam Jan 26 at 18:42
  • I am confused about the purpose of an infinite for loop. I suspect a while loop would be more appropriate. – Aaron Jan 27 at 19:12
-1

Another approach could be just OGR2OGR and not use python. Also you can add your batch script to windows scheduler to run at a set schedule or just double click .bat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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