import arcpy import os def fcs_in_workspace(workspace): arcpy.env.workspace = workspace for fc in arcpy.ListFeatureClasses(): yield os.path.join(workspace, fc) for ws in arcpy.ListWorkspaces(): for fc in fcs_in_workspace(os.path.join(workspace, ws)): yield fc for fc in fcs_in_workspace("C:/Data"): ...
fclist = list(fcs_in_workspace("C:/Data"))
Hi All,
I wanted to add to additional ways to accomplish this and apply it to different data types.
I teach Python for Esri and have had this question come up in class alot.
1. Arcpy.da.walk:
This was introduced to the data access module at 10.1 SP1
ArcGIS Help (10.2, 10.2.1, and 10.2.2)
Where this function is nice is in 2 parameters
Datatype: the value set here is the filter for what is returned as walk traverses the subfolders
type: not for all datasets, but at least you could specify point, line, poly or raster types
Look at the Sample script 1 in the documentation
2. python os.walk:
this function will perform the same type operation gathering subfolders and files
The one difference is this won't retrieve contents inside of a geodatabase.
This could be used if you script involved python code with out the use of arcpy
maybe you needed to find all the .xls files under a \projects folder.
15.1. os — Miscellaneous operating system interfaces — Python 2.7.8 documentation
Thanks,
Jeff
import arcpy import os def fcs_in_workspace(workspace): arcpy.env.workspace = workspace for fc in arcpy.ListFeatureClasses(): print os.path.join(workspace, fc) for ws in arcpy.ListWorkspaces(): fcs_in_workspace(os.path.join(workspace, ws)) fcs_in_workspace("C:/Data")
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.