0

I'm following very closely this topic Merging all feature classes with same name nested in multiple geodatabases using ArcPy?, it seems very useful to my python script

this is the script I following from made by Emil Brundage (answered Sep 8 '17 at 15:21):

import arcpy
import os

workspace = "C:/Wiley/P1/gis"
outdir = "C:/Wiley/P1/gis/HK80.gdb"

walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")
di = {}

for dirpath, dirnames, filenames in walk:
          for filename in filenames:
             fcFullPath = os.path.join(dirpath, filename)
             #try adding feature class to dictionary with already-existing key
             try: di [filename] += [fcFullPath]
             #key not in dictionary yet. Create key
             except: di [filename] = [fcFullPath]

#iterate dictionary
for filename in di:
    #get merge feature classes
    mergeFcs = di [filename]
    output = os.path.join(outdir, os.path.basename(fcFullPath) + "_merge")
    arcpy.Merge_management(mergeFcs, output)

However, when I run the script I get a message error show up, a feature class already exists. The script does exactly what I want, but how could I fix the script to avoid this error type.

Following the suggestion from amasephy add the arcpy.Exists and arcpy.Delete_management function on my script:

workspace = r'...\GDBESTATALES_PRUEBA'
outdir = r'...\GDB_Nacional.gdb'

walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")
di = {}

for dirpath, dirnames, filenames in walk:
          for filename in filenames:
             fcFullPath = os.path.join(dirpath, filename)
             #try adding feature class to dictionary with already-existing key
             try: di [filename] += [fcFullPath]
             #key not in dictionary yet. Create key
             except: di [filename] = [fcFullPath]

#iterate dictionary
for filename in di:
    #get merge feature classes
    mergeFcs = di [filename]
    output = os.path.join(outdir, os.path.basename(fcFullPath) + "_merge")
    arcpy.Merge_management(mergeFcs, output)
    if arcpy.Exists(output):
     arcpy.Delete_management(output)

However, nothing happens what I want it, the only one happens is the script takes the last geodatabase and save the last feature class in the other feature class (GDB_Nacional.gdb), that's all.

1 Answer 1

3

Try using the arcpy.Exists function. If the output name exists, then append a numerical digit to the output. Alternatively, use arcpy.Delete_management to delete the feature that already exists.

5
  • Thaks for answer, I´m gonna try, Thanks for you help Commented Feb 27, 2018 at 19:51
  • Dear amasephy. I tryed to use arcpy.Exists function and arcpy.Delete_management, nothing happens what I want it, the only one happens is the script takes the last geodatabase and save the last feature class in the other feature class (GDB_Nacional.gdb), thats all. Commented Feb 27, 2018 at 23:12
  • Can you post the code you tried with the functions I posted above?
    – amasephy
    Commented Feb 28, 2018 at 11:35
  • Dear amasephy, already I posted my script modified with the functions, I don't know where put the functions Commented Feb 28, 2018 at 15:22
  • You need to put the test for an existing feature class before the merge function.
    – amasephy
    Commented Feb 28, 2018 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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