[GIS] Executing buffer analysis in ArcPy with for loop

arcpyfor loop

I'm attempting to run the Buffer Analysis tool with a distance of 3 km over multiple shapefiles at the same time. I was given a folder with different types of files and ran a for loop to create a new list with just the files ending with ".shp" that I have below.

import os
import arcpy
from arcpy import env 
env.workspace = "file_path"
env.overwriteOutput = True
items = os.listdir("file_path")
 newlist = []

for names in items:
    if names.endswith(".shp"):
        newlist.append(final)
print newlist

Thus, this creates my list with just the shapefiles. However, this is where I struggle. I'm not sure how to run the buffer analysis tool over this new list that I've created.

Best Answer

Arcpy has a special method called ListFeatureClasses that will list all the feature classes in the workspace. If the workspace is a folder, then it is likely the feature classes will be shapefiles. You can use the list in a loop. Then apply the buffer analysis. The trick will be getting the outname.

import arcpy
from arcpy import env 
env.workspace = "file_path"
env.overwriteOutput = True
fcs = arcpy.ListFeatureClasses():

for fc in fcs:
    arcpy.Buffer_analysis(fc, OutputName, "100 Feet", "FULL", "ROUND", "LIST", "Distance")

Here is the documentation on buffers: http://desktop.arcgis.com/en/arcmap/10.3/tools/analysis-toolbox/buffer.htm

Related Question