[GIS] Clip analysis in ArcPy

arcpyclip

I have one shapefile that covers an entire city. I also have a list of 100 shapefiles which are buffers in different places in the city. Now I want to clip for each buffer from that city using ArcMap and python. I tried using arcpy in python but the code is not working.

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "U:\Park and Residential Area\Test\SBA park_res_buffer_5\SBA.gdb"
infeature= "U:\Park and Residential Area\Test\park_res_merge.shp"
clipfeatture = arcpy.ListFeatureClasses("*", "polygon")
for i in clipfeatture:
    outclipfeatture = arcpy.Clip_analysis(infeature,i)
    outclipfeatture.save("U:\Park and Residential Area\Test\SBA park_res_buffer_5/"*i)

This is the error i get:

Traceback (most recent call last):
File "<pyshell#10>", line 3, in <module> outclipfeatture.save("U:\Park and Residential Area\Test\SBA park_res_buffer_5/"*i)
AttributeError: 'Result' object has no attribute 'save'

Best Answer

arcpy.Clip_analysis() has 3 required parameters: in_features, clip_features, and out_feature_class. You're only supplying the first two. Additionally, there is no save method for the Result object, as your error message indicates.

Instead, you should set outclipfeatture = r"U:\path\to\output.shp" (which will need to be unique for each clip feature) and then call arcpy.Clip_analysis(infeature, i, outclipfeatture). This will create the output shapefile and save the result to it.


One other note: your current paths will not be interpreted correctly. You either need to convert them to raw strings by placing an r in front of the string as in my example, change your backslashes (\) to forward slashes (/), or double them up (\\) so they are properly escaped.

Related Question