Wednesday 22 April 2015

arcpy - Listing Feature Classes of Multiple geodatabase in Multiple Folder?


I'm new in python scripting and try to list polyline feature classes of multiple Geodatabases. The Geodatabases are in many nested folders.For example in folder "A" I have 20 folders and in each folder i have 3 geadatabases. the question is that how can i list the feature classes of the geodatabases using Arcpy. I know simple Python codes to list feature classes in a folder such as :


import arcpy
from arcpy import env

env.workspace = ".././Data/6"
fclist = arcpy.ListFeatureClasses()
print fclist

I surf in this site and found a Q&A about How to list feature classes in multiple Geodatabases in a folder? . It was very useful but only for multi Geodatabases not multi folders.



Answer



The arcpy.da.Walk function from ArcGIS 10.1 SP1 allows you to do this. The following script walks through a workspace, create a list of every polyline, and copies the polylines to an output workspace.


import arcpy
import os


in_workspace = r"C:\your\path"
out_workspace = r"C:\your\path2\temp.gdb\fds"

feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="Polyline"):
for filename in filenames:
feature_classes.append(os.path.join(dirpath, filename))

print feature_classes


# Loop through the "feature_classes" list and copy featureclasses to out_workspace
for fc in feature_classes:
name = os.path.basename(fc) # Extract only the FC basename
arcpy.CopyFeatures_management(fc, os.path.join(out_workspace, name))

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...