2

I need some help with ArcGIS python script

My folder tree is like this:

/Testing location

C1

    POIS.shp
    STREETS.shp
C2

etc, etc

I'm trying to make a script to run from arcgis that would go into each folder from the main directory and buffer the two shapefiles, output a specific file (Surrounding_Streets.shp for example) for each layer in each respective folder.

So within each folder I'd now have POIS.shp, POIS_SURROUNDING.shp, STREETS.shp, and STREETS_SURROUNDING.shp

Aaron
  • 51,658
  • 28
  • 154
  • 317
John
  • 63
  • 1
  • 6

2 Answers2

2

See the ArcGIS Desktop help topic on Listing data for how to access feature classes (shapefiles) inside workspaces (subfolders).

Generally, you will need to set the main env.workspace, loop through folders = arcpy.ListWorkspaces('*', 'Folder'), reset the env.workspace to the 'folders' list item, and use another loop fcs = arcpy.ListFeatureClasses(wildcard), where 'wildcard' corresponds to your specific shapefiles (which can also be a list that is looped through). Tends to be slow...

If you would like to use a faster python module for listing the subfolders try os.listdir. And a somewhat similar post to yours can be seen here using os.walk.

My favorite is glob with the relevant wildcard (e.g. '*.shp') - a subfolder/sudirectory example is shown in this PyMOTW - glob. Below is code specific to your shapefiles:

import arcpy
import glob
main_dir = r'C:\Testing\location'
# Loop through all files by wildcard
for wildcard in ['POIS.shp', 'STREETS.shp']:
    # Specify the subdirectory variable and specific shapefile name to match
    sub_dir = main_dir + '/*/' + wildcard
    # Loop through files in subdirectory
    for in_fc in glob.glob(sub_dir):
        # Specify output variable
        out_fc = in_fc.rstrip('.shp') + '_Surrounding.shp'
        # Buffer - insert your own parameters here
        arcpy.Buffer_analysis(in_fc, out_fc, "100 Meters", "FULL", "ROUND", "ALL")

You could use an attribute field for the distance value or specify this using a dictionary (instead of the wildcard list in the example) in case you need different buffer distances for each shapefile type.

ccn
  • 2,919
  • 20
  • 19
  • 2
    I agree with @ccn except that rather than use os.walk or glob, if you are using ArcGIS 10.1 SP1 (or later) then using arcpy.da.Walk instead would mean only having to import arcpy. – PolyGeo Feb 13 '13 at 22:23
  • Thanks! Sorry for late reply, but I got the job done using os.listdir. – John Mar 14 '13 at 17:43
0

I am a big fan of using the glob module for this sort of work as @ccn has shown, however, arcpy.da.Walk() was designed for this sort of task. For example:

import arcpy, os

workspace = r"C:\temp"

walk = arcpy.da.Walk(workspace, datatype="FeatureClass")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        print dirpath
        print filename
        infc = os.path.join(dirpath, filename)
        outfc = os.path.join(dirpath, filename.split(".")[0] + "_SURROUNDINGS")
        arcpy.Buffer_analysis (infc, outfc, 10) # Buffer by 10m for example
Aaron
  • 51,658
  • 28
  • 154
  • 317