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.

2 Answers 2

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))
8
  • nice.is there any method without using sp1 ? thnx
    – BBG_GIS
    Commented Apr 28, 2013 at 15:30
  • I could not use this function to copy feature classes using arcpy.CopyFeatures_management How can i use this function to copy the list features ? thnx
    – BBG_GIS
    Commented Apr 28, 2013 at 19:22
  • 1
    @wetland post edited to address your comment. I hope this helps.
    – Aaron
    Commented Apr 28, 2013 at 22:43
  • it's not working . I just use your edited answer in loop of arcpy.da.Walk
    – BBG_GIS
    Commented Apr 29, 2013 at 10:05
  • I just tested the script again--all is well. What errors are you receiving?
    – Aaron
    Commented Apr 29, 2013 at 12:13
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 = []

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.