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.