A simple way to do it might be:
import arcgisscripting
import sys
import glob
FOLDER_CONTAINING_SHAPEFILES = "c:\\folder\\containing\\blah\\"
gp = arcgisscripting.create()
# Set a default workspace
gp.workspace = "d:/MA_resevior"
# An input polygon feature class
for fc in glob.glob(FOLDER_CONTAINING_SHAPEFILES + "*.shp"):
inputFC = fc
gp.AddField_management(
inputFC, "new_area", "DOUBLE")
gp.CalculateField_management(inputFC, "new_area", "float(!
SHAPE.AREA!)", "PYTHON")
On 18 May 2010 14:29, Jen <[email protected]> wrote:
> Thank you for the comments, that's very helpful.
>
> I have 56 shapefiles, and I need to calculate area for each one.
> Therefore, could you explain the first method clearer: "to use the
> script as is, run it from the command line with the full path to the
> shapefile you want to calculate the area, e.g. from the command
> line:" ?
>
> I'd appreciate any help you might give me.
>
> Jen
>
> On May 17, 9:50 pm, Dylan Hettinger <[email protected]> wrote:
> > sys.argv is a Python list of arguments. The first value, sys.argv[0], is
> > always the current script. You're probably running this script from the
> > command line (or an editor like IDLE) without giving it a shapefile as an
> > argument. To use the script as is, run it from the command line with the
> > full path to the shapefile you want to calculate the area, e.g. from the
> > command line:
> >
> > > C:\python26\python.exe D:\MA_resevior\LU_PL_Exportcoef.py
> >
> > C:\path\to\your\shapefile.shp
> >
> > You could also change the line:
> > inputFC = sys.argv[1]
> > to:
> > inputFC = "C:\\path\\to\\your\\shapefile.shp"
> >
> > This way it won't expect an argument, but you'll have to change that line
> > every time you want to use a different shapefile.
> >
> > dylan
> >
> > On Mon, May 17, 2010 at 6:57 PM, Jen <[email protected]> wrote:
> > > There is some existing code for calculating shapefile area with
> > > python, as I listed below.
> >
> > > import arcgisscripting
> > > import sys
> >
> > > gp = arcgisscripting.create()
> >
> > > # Set a default workspace
> > > gp.workspace = "d:/MA_resevior"
> >
> > > # An input polygon feature class
> > > inputFC = sys.argv[1]
> >
> > > gp.AddField_management(inputFC, "new_area", "DOUBLE")
> > > gp.CalculateField_management(inputFC, "new_area", "float(!
> > > SHAPE.AREA!)", "PYTHON")
> >
> > > However, python shows error message:
> >
> > > Traceback (most recent call last):
> > > File "D:\MA_resevior\LU_PL_Exportcoef.py", line 16, in <module>
> > > inputFC = sys.argv[1]
> > > IndexError: list index out of range
> >
> > > If I change sys.argv[1] to sys.argv[0], and print sys.argv[0], it
> > > shows D:\MA_resevior\LU_PL_Exportcoef.py, which is my python code
> > > file. It shows error message:
> >
> > > Traceback (most recent call last):
> > > File "D:\MA_resevior\subbasin_LU\LU_PL_Exportcoef.py", line 24, in
> > > <module>
> > > gp.AddField_management(inputFC, "new_area", "DOUBLE")
> > > ExecuteError: Failed to execute. Parameters are not valid.
> > > ERROR 000732: Input Table: Dataset D:\MA_resevoir\LU_PL_Exportcoef.py
> > > does not exist or is not supported
> > > Failed to execute (AddField).
> >
> > > I'd appreciate any help you might give me.
> >
> > > Jen
>