0

I need to calculate the shape area (and possible the perimiter) of polygons for over 300 files. I tried to use a Python code but I am not very familiar with Python so when it returns an error I do not know where the problem is. My Python code looks like this:

import arcpy
from arcpy import env
import os
import csv

outputFilePath = 'outputPath'
sourceFolderPath = 'inputPath'


for root, dirs, files in arcpy.da.Walk(sourceFolderPath):
    for name in files:
        if name[-4:] == '.shp':
            arcpy.env.workspace = os.path.join(root, name)
            arcpy.AddField_management(name[:-4], "Shape_area", "DOUBLE")
            arcpy.CalculateField_management(name[:-4], outputFilePath + '/' + name[:-4], "Shape_area", "PYTHON_9.3")

I received this error message:

Traceback (most recent call last):
  File "<string>", line 15, in <module>
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 3290, in CalculateField
    raise e
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 3287, in CalculateField
    retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block), True)))
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.

Any suggestions?

Vince
  • 20,017
  • 15
  • 45
  • 64
Malna
  • 1
  • 1
  • I changed [name[:-4]] to name[:-4], thanks, good point. Unfortunately I still get error. A new one saying that File "<string>", line 15, in <module>, and Parameters are not valid. – Malna Mar 04 '20 at 08:39
  • Thanks @ahmadhanb, I change it. – Malna Mar 04 '20 at 08:44
  • can you please let me know the format of files you are using i.e shapefiles,feature classes or dbf? – Asad Abbas Mar 04 '20 at 09:51
  • I think what you are looking for is Calculate Geometry attributes please refer to the following link https://pro.arcgis.com/en/pro-app/tool-reference/data-management/calculate-geometry-attributes.htm – Asad Abbas Mar 04 '20 at 10:07

2 Answers2

2

Make sure you get correct syntax for Calculate Field. It is best to include parameter names (for example in_table='blablabla') instead of just relying on order/index of parameters:

arcpy.CalculateField_management (in_table="sometable", field="somefield", expression="someexpression",...)

For example you are providing outputFilePath + '/' + name[:-4] as field name, this cant be correct?

I never use CalculateField in code, so easy to get wrong. Try the much more versatile da.UpdateCursor:

with arcpy.da.UpdateCursor(name[:-4], ["Shape_area", "SHAPE@AREA"]) as cursor:
    for row in cursor:
        row[0] = row[1]
        cursor.updateRow(row) 
BERA
  • 72,339
  • 13
  • 72
  • 161
0

I checked first for one file the code here Adding a new field and calculating area using Python , then I edited my code:

import arcpy
from arcpy import env
import os

sourceFolderPath = 'MyPath'


for root, dirs, files in arcpy.da.Walk(sourceFolderPath):
    for name in files:
        if name[-4:] == '.shp':
            arcpy.env.workspace = os.path.join(root, name)
            arcpy.AddField_management(name,"area","Double")
            expression1 = "{0}".format("!SHAPE.AREA@SQUAREMETERS!")        
            arcpy.CalculateField_management(name, "area", expression1, "PYTHON")
Malna
  • 1
  • 1