0

I am trying to calculate summary statistics for a number of shapefiles, which is easy enough, but the field name for each shapefile is different. The field name is identical to the file name if that makes things easier.

I'm trying to use the following script:

import arcpy, os
from arcpy.sa import *

arcpy.env.workspace = r'C:\Users\kellyj\Desktop\Projects\CostAnalysis\SHP'

for file in arcpy.ListFiles("*.shp"):
    arcpy.env.overwriteOutput = 1
    filename = file[0:-4]
    print "Calculating " + filename
    arcpy.Statistics_analysis(file, arcpy.env.workspace + "/" + filename, [[filename, "SUM"]], "")

print "Done"

How can I write this script so that I can use the file name as the statistics field?

  • 1
    It seems like you have done that already. What problem are you encountering? – Aaron Sep 5 '14 at 17:05
1

I think your logic is a bit flawed you appear to be writing back into the same folder and giving it the name of the shapefile, which makes no sense?

Make a subfolder called Output here:

C:\Users\kellyj\Desktop\Projects\CostAnalysis\Output

Then try this code:

import arcpy, os
from arcpy.sa import *

arcpy.env.workspace = r'C:\Users\kellyj\Desktop\Projects\CostAnalysis\SHP'
output = r"C:\Users\kellyj\Desktop\Projects\CostAnalysis\Output"

for file in arcpy.ListFiles("*.shp"):
    arcpy.env.overwriteOutput = 1
    filename = file[0:-4]
    print "Calculating " + filename
    arcpy.Statistics_analysis(file, output + "/" + filename + ".dbf", [[filename, "SUM"]], "")

print "Done"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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