I am using arcpy and I want to extract a specific feature class called "building" nested in a number of geodatabase within my workspace.
Here is the code:
import arcpy
import os
workspace = "C:/Wiley/P1/gis"
search = "Building"
outdir = "C:/Wiley/P1/gis/HK80.gdb"
fc = []
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon")
for dirpath, dirnames, filenames in walk:
for filename in filenames:
if search == filename:
fc.append(os.path.join(dirpath, filename))
if fc:
output = os.path.join(outdir, os.path.basename(search) + "_merge")
arcpy.Merge_management(fc, output)
This code was successful.
But there are many other featureclass in the geodatabase apart from "Building" So I expanded the code a bit and try to loop the merge command through all these feature class:
import arcpy
import os
#--------------------------
#example list workspace
arcpy.env.workspace = "C:/Wiley/P1/gis/HKU_Job69.gdb"
#Generate exmaple list dataset
datasets = arcpy.ListDatasets(feature_type='All')
datasets = [''] + datasets if datasets is not None else []
fc_list = []
#--------------------------
#where all the gdb is nested
workspace = "C:/Wiley/P1/gis"
#define output
outdir = "C:/Wiley/P1/gis/HK80.gdb"
#List of Feature classes
fc = []
#define walk for looping
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Any")
for ds in datasets:
for fc_example in arcpy.ListFeatureClasses(feature_dataset=ds):
fc_list.append(fc_example)
for f in fc_list: #Loop through the list
for dirpath, dirnames, filenames in walk:
for filename in filenames:
if f == filename:
fc.append(os.path.join(dirpath, filename))
if fc:
output = os.path.join(outdir, os.path.basename(search) + "_merge")
arcpy.Merge_management(fc, output)
This is where I hit a wall. Is there anything wrong with this code? I am new to Python so for a trained eye you guys may spot out the error in the logic immediately. The f variable didn't seem to loop at all.