2

The script below is functional, but it runs into issues if it comes across duplicate files (even when overwriteOutput=True) and corrupt shapefiles when running feature to feature.

Are there exceptions I can add that will either skip these features if they already exist in the database, or continue running the script if it runs into an error adding a shapefile to a geodatabasing?

This is the script that I am trying to add the error handling to. The script performs the following actions:

  1. Walk through a directory and compile a list of shapefiles
  2. Add the shapefiles to a geodatabase

import arcpy, os, fnmatch
from arcpy import env

env.overwriteOutput=True

rootDir = "whatever directory"
search = "whatever criteria"
outGDB = "whatever GDB"

shpLst = []
    for root, dirs., files in os.walk(rootDir, topdown=True):
        for filename in fnmatch.filter(files, search):
            shpLst.append(os.path.join(root, filename))

for fc in shpLst:
    date = os.path.basename(fc).split("_")[0]
    outName = os.path.basename(fc)[9:].replace(".shp", "_") + date
    arcpy.FeatureClassToFeatureClass_management(fc, outGDB, outName, "", "", "")
    print outName + " sent to " + outGDB
  • What is your code supposed to do? What precisely does it do "wrong" when you run it? What do you mean by "databasing a feature"? – PolyGeo May 28 '16 at 8:42
  • It traverses a directory tree looking for specific shapefiles, and sends them to a file gdb. If it runs into a shapefile that is corrupt, it tries to database it anyways and the tool fails, rather than skipping it. checking/repairing geometry didn't work. Also, if something is already in the database with that name, it fails rather than overwriting it. Specifically error 000210 Cannot create output – geodranic May 28 '16 at 8:44
  • 1
    Please use the edit button beneath your question to revise it with these details. At the moment you are describing two problems with your current code. I think you should focus this question on the one about dealing with two final datasets of the same name. I also think that you should undelete and edit your question about invalid shapefiles so that we can complete processing it because that seems to be your second problem/question. – PolyGeo May 28 '16 at 9:47
3

You can use Exists (arcpy) to check for duplicates in your geodatabase. For example:

import arcpy, os, fnmatch
from arcpy import env

env.overwriteOutput=True

rootDir = "whatever directory"
search = "whatever criteria"
outGDB = "whatever GDB"

shpLst = []
for root, dirs., files in os.walk(rootDir, topdown=True):
    for filename in fnmatch.filter(files, search):
        shpLst.append(os.path.join(root, filename))

for fc in shpLst:
    date = os.path.basename(fc).split("_")[0]
    outName = os.path.basename(fc)[9:].replace(".shp", "_") + date
    if not arcpy.Exists(os.path.join(outGDB, outName)):
        arcpy.FeatureClassToFeatureClass_management(fc, outGDB, outName, "", "", "")
        print outName + " sent to " + outGDB
    else:
        print "%s exists already, moving on..." % outName

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.