Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

Thanks for any help!

share|improve this question
add comment

1 Answer

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.

share|improve this answer
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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