I have tree feature class with the same name in different geodatabases for example:
one.gdb\fc1,fc2,fc3 | two.gdb\fc1,fc2,fc3 | tree.gdb\fc1,fc2,fc3
what I want to build a single merge in another geodatabase, for example, result.gdb\fc1_merge, fc2_merge. fc3_merge. ¿How could I build a script python to merge the fcs in one single fc in another geodatabase?
So far, this is my script:
import arcpy, os, sys, string, fnmatch
import arcpy.mapping
from arcpy import env
arcpy.env.workspace = 'C:\FOLDER_GDB' #<--- All my gdb inside
inWorkspace = arcpy.env.workspace
OutPutWorkspaces = 'C:\FOLDER_RESULT\Result.gdb'
fc = 'fc1'
Matches = []
for root, dirs, files in os.walk(inWorkspace):
for shps in files:
if shps == 'fc1':
match =(os.path.join(root, shps))
matches.append (match)
arcpy.Merge_management(Matches,OutPutWorkspaces+fc+'_Merge')
print 'Done!!'
When I run the script I get de next error:
Traceback (most recent call last):
File "<string>", line 254, in run_nodebug
File "D:\SedesolVQB\Merge_prueba.py", line 18, in <module>
arcpy.Merge_management(Matches,OutPutWorkspaces+fc+'_Merge.shp')
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 3816, in Merge
raise e
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000735: Input Datasets: Value is required
Failed to execute (Merge).
Does anybody can help me to fix my script
1 Answer 1
You cannot use os.walk to iterate through a gdb, rather use a combination of os.walk to find directories with gdb extension and then use arcpy.da.Walk to iterate through the gdbs. Something like this should work:
OutPutWorkspaces = 'C:/FOLDER_RESULT/Result.gdb'
matchFC1 = []
matchFC2 = []
matchFC3 = []
for root, dirs, files in os.walk('C:/FOLDER_GDB'):
for d in dirs:
foundGDB = d.find('.gdb')
if foundGDB > -1:
for dirpath, dirnames, filenames in arcpy.da.Walk(root +'//'+d,datatype="FeatureClass"):
for file in filenames:
if file == 'fc1':
matchFC1.append(file)
if file == 'fc2':
matchFC2.append(file)
if file == 'fc3':
matchFC3.append(file)
if matchFC1:
arcpy.Merge_management(matchFC1,OutPutWorkspaces+'//FC1_Merge')
if matchFC2:
arcpy.Merge_management(matchFC2,OutPutWorkspaces+'//FC2_Merge')
if matchFC3:
arcpy.Merge_management(matchFC3,OutPutWorkspaces+'//FC3_Merge')