Problems iterating through a list inside a toolbox function using python.

563
3
10-21-2013 09:22 AM
IanSchelly
New Contributor
I have a point file and I need to make a lot of buffers in specific increments but I can't figure out how to interate through a list inside the buffer function. Here is what I have:
import arcpy
city_pts = "C:\Urban_Land_Project\Cities\WI_cities.shp"
buffer_sizes = [1,2,4,7,10,15,20,30]
for x in buffer_sizes:
     arcpy.Buffer_analysis(city_pts, "C:\Urban_Land_Project\City_buffers\WI_city_buffers*km.shp","2 Kilometers" ,"FULL","ROUND","NONE","      #")
I need to have each buffered file to be named "WI_city_buffers*km.shp" with the * being the buffer size. How do I get the arcpy.Buffer_analysis to go through my list?
Tags (2)
0 Kudos
3 Replies
MikeMacRae
Occasional Contributor III
I have a point file and I need to make a lot of buffers in specific increments but I can't figure out how to interate through a list inside the buffer function. Here is what I have:
import arcpy
city_pts = "C:\Urban_Land_Project\Cities\WI_cities.shp"
buffer_sizes = [1,2,4,7,10,15,20,30]
for x in buffer_sizes:
     arcpy.Buffer_analysis(city_pts, "C:\Urban_Land_Project\City_buffers\WI_city_buffers*km.shp","2 Kilometers" ,"FULL","ROUND","NONE","      #")
I need to have each buffered file to be named "WI_city_buffers*km.shp" with the * being the buffer size. How do I get the arcpy.Buffer_analysis to go through my list?


This should work:
r"C:\Urban_Land_Project\City_buffers" + "\\" + "WI_city_buffer" + str(x) + "km.shp"
0 Kudos
IanSchelly
New Contributor
Thanks a lot, this is going to help me out a lot in this coming week!
0 Kudos
JasonScheirer
Occasional Contributor III
import arcpy
city_pts = "C:\Urban_Land_Project\Cities\WI_cities.shp"
buffer_sizes = [1,2,4,7,10,15,20,30]
for distance in buffer_sizes:
    out_shape = "C:\Urban_Land_Project\City_buffers\WI_city_buffers{}km.shp".format(distance)
    out_km = "{} Kilometers".format(distance)
    arcpy.Buffer_analysis(city_pts, out_shape, out_km, "FULL", "ROUND", "NONE", "#")
0 Kudos