3

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.

BBG_GIS
  • 5,825
  • 4
  • 45
  • 88

2 Answers2

5

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))
Aaron
  • 51,658
  • 28
  • 154
  • 317
1

I created the following function in Python that takes as input a starting folder and populates a global list with all the sub-folder paths. You can then step through this using the arcpy.ListWorkspaces()

import arcpy
import os

listFolders = []

def GetFolders(root):
    global listFolders
    try:
        l = os.listdir(root)
        for x in l:
            path = root + "\\" + x
            if os.path.isdir(path):
                listFolders.append(path)
                GetFolders(path) # Recursive call here!
    except Exception as e:
        print e
        # An error occurred set list to empty
        listFolders = []
Hornbydd
  • 43,380
  • 5
  • 41
  • 81