I have a geodatabase with many polyline feature classes all with different names. I would like to write a code in python ArcGIS 10.1 to use a geoprossing tool on all classes in the gdb. How would this be done?
Would the same logic work for a folder of shapefiles?
For this question lets say I want to use Create Random Points
500 metres apart on all polylines in this gdb.
import arcpy
from arcpy import env
arcpy.env.workspace = "E:\Example.gdb"
arcpy.CreateRandomPoints_Management (
I asked a similar question on Stack Overflow, except using VBA to loop through a folder of excel workbooks.
https://stackoverflow.com/questions/22022672/looping-a-code-through-a-folder-of-workbooks-with-vba
2 Answers 2
Try something like this:
import arcpy
from arcpy import env
env.workspace = "E:\Example.gdb"
# here you are creating a list of all features from your workspace
lst = arcpy.ListFeatureClasses("", "Polyline")
# and here you are looping
for featureClass in lst:
arcpy.CreateRandomPoints_Management(
-
2Don't use "list" as a variable name (i.e.
list = arcpy.ListFeatureClasses("", "Polyline")
). A list is a python built-in type.user2856– user28562014年04月23日 03:08:51 +00:00Commented Apr 23, 2014 at 3:08
I would simply set my workspace (arcpy.env.workspace = "NAME OF GDB HERE") and then list the Line feature classes into a python list (fcs = arcpy.ListFeatureClasses("","Polyline")). Then you can loop through them.
So same as other answer provided but with 1 minor difference - incorporating the Polyline option:
import arcpy
arcpy.env.workspace = "C:\Your.gdb"
# Create list of all polyline feature classes
fcs = arcpy.ListFeatureClasses("", "Polyline")
for fc in fcs:
arcpy.CreateRandomPoints_Management(
Some documentation here:
http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000018000000