Saturday, 1 April 2017

arcpy - Using feature class name as name in output (buffer_analysis)


I'm pretty new in python, and I'm trying to iterate through a list of feature classes, and perform a buffer on each feature class. So I've done this:


import arcpy  
from arcpy import env
env.workspace = "C:/EsriPress/Python/Data/Midterm/Oil_project.gdb"
env.overwriteOutput = True
fclist = arcpy.ListFeatureClasses("", "Polyline", "Well_Data")

for fc in fclist
buffer = arcpy.Buffer_analysis(fc, fcname_buffer, "75 FEET")


However, I want each output file name to be the original feature class name with _buffer at the end. For example, the name of the first feature class in the list is Well_A_BB05.shp. I want the output to be "Well_A_BB05_buffer". I can't get this to work.



Answer



In the arcpy.Buffer_analysis() set your output to be '{}_buffer'.format(fc) which will add your feature class name from fc in front of the _buffer text e.g. if your feature class is called "Line3" the output buffer feature class would be called "Line3_buffer"


import arcpy  
from arcpy import env
env.workspace = "C:/EsriPress/Python/Data/Midterm/Oil_project.gdb"
env.overwriteOutput = True

fclist = arcpy.ListFeatureClasses("", "Polyline", "Well_Data")


for fc in fclist:
arcpy.Buffer_analysis(fc, '{}_buffer'.format(fc), "75 FEET")

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...