1

I'm trying to run a script in arcpy and appears the error 000725 "output folder: Dataset C:.. already exists.

The script objective is to clip all shapefiles from a folder by a buffer layer. If the shapefiles clipped are polygons, area is calculated. And I'm trying to export all the Excel tables from these shapefiles with a column called ("NEW_AREA"). I used the arcpy.env.overwriteOutput=True to fix the error but doesn't work. What am I doing wrong?

My script:

import arcpy
pol = arcpy.GetParameterAsText(0)
buf = arcpy.GetParameterAsText(1)
output_buf = arcpy.GetParameterAsText(2)
input_folder = arcpy.GetParameterAsText(3) 
output_folder = arcpy.GetParameterAsText(4)

arcpy.env.workspace = input_folder
arcpy.env.overwriteOutput=True
list_fc = arcpy.ListFeatureClasses()
fc = os.listdir(input_folder)

clip_features = arcpy.Buffer_analysis(pol,output_buf,buf)


def CLIP(input_folder, output_folder, clip_features):
    for file in fc:
            for i in fc:
                if i.endswith(".shp"):
                    file = os.path.join(input_folder,i)
                    rename_shp = "clipped_%s" % (i)
                    rename_excel = "calculated%s" % (i)
                    newfile = os.path.join(output_folder,rename_shp)
                    newtable = os.path.join(output_folder,rename_excel)
                    desc = arcpy.Describe(file)
                    geometry = desc.shapeType
                    if geometry == 'Polygon':
                        fieldName = "NEW_AREA"
                        expr ='!shape.area@squaremeters!'
                        arcpy.Clip_analysis(file, clip_features, newfile, "") and arcpy.management.AddField(newfile,"NEW_AREA","DOUBLE") and arcpy.CalculateField_management(newfile,fieldName,expr,'PYTHON') and arcpy.TableToExcel_conversion(newfile,newtable,"NEW_AREA")
                    else:
                        arcpy.Clip_analysis(file, clip_features, newfile, "")

CLIP(input_folder, output_folder, clip_features)
3
  • 1
    There are no Excel files associated with a shapefile. The dBase (.dbf) file of a shapefile should be kept well clear of Excel, so you don't accidentally corrupt it.
    – Vince
    Commented Feb 9, 2023 at 19:45
  • 2
    That wasn't an answer, it was a comment. Another comment: Your code structure indicates you need to review basic Python coding -- There's no real value in making the main loop inside a function using variables from above; You have a double-nested loop on fc which is almost certainly not necessary; The use of and after the Clip directive is not best practice, and probably doesn't do what you think it does (can you explain why you're using it?). Comment out all the arcpy code and see if you can make the Python follow your expected flow of control. Then start coding the geoprocessing.
    – Vince
    Commented Feb 10, 2023 at 0:15
  • 1
    Other issues include no import os statement. No xls extension on for the output excel table. "NEW_AREA" in the TableToExcel. Using both ListFeatureClasses and listdir when you only need one method to list the folder content. Commented Feb 10, 2023 at 13:03

1 Answer 1

2

You code could be cleaned up like this.

import os #import os was missing
import arcpy
pol = arcpy.GetParameterAsText(0)
buf = arcpy.GetParameterAsText(1)
output_buf = arcpy.GetParameterAsText(2)
input_folder = arcpy.GetParameterAsText(3)
output_folder = arcpy.GetParameterAsText(4)

arcpy.env.overwriteOutput=True
arcpy.env.workspace = input_folder
clip_features = arcpy.Buffer_analysis(pol,output_buf,buf)

# Only need one of ListFeatureClasses or listdir
list_fcs = arcpy.ListFeatureClasses()
for fc in list_fcs:
    # have a look at the shapefile name
    print(fc)
    rename_shp = "clipped_{}".format(fc)
    # make sure to replace the .shp with a .xls
    rename_excel = "calculated_{}.xls".format(os.path.splitext(fc)[0])
    # have a look at the new names you are going to use for output
    print(rename_shp)
    print(rename_excel)
    newfile = os.path.join(output_folder,rename_shp)
    newtable = os.path.join(output_folder,rename_excel)

    # you are doing the same clip, so move it oustide the if statement
    arcpy.Clip_analysis(fc, clip_features, newfile) 
    desc = arcpy.Describe(fc)
    geometry = desc.shapeType
    if geometry == 'Polygon':
        fieldName = "NEW_AREA"
        expr ='!shape.area@squaremeters!'
        # remove the anding together of functions
        arcpy.management.AddField(newfile,fieldName,"DOUBLE")
        # if you are using variable for the field name, remove the hard coded "NEW_AREA"
        arcpy.CalculateField_management(newfile,fieldName,expr,'PYTHON')
        # remove the "NEW_AREA" from TableToExcel as this is not  valid parameter value
        arcpy.TableToExcel_conversion(newfile,newtable)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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