Iterating feature classes and writting in a text file

5294
21
Jump to solution
05-30-2015 02:10 AM
KONPETROV
Occasional Contributor III

Hi i am trying to export some values from a number of shapefiles to a text file so as to use it to creat a point shapefile. The process is succeded ONLY when i put my files in the table of context. Why is that? **I have only folders no GDBase or .mxd**

In addition there is A problem with my code cause i can't get the right calculation of AVGDISTANCES i put it outside if and for, but nothing changed. FOR EXAMPLE for DISTANCE = 170, 170, 160, 150 i should get AVGDISTANCES= 162.5 not 150.  This is my code:

 import arcpy
    import os
    from arcpy import env
    Routesworkspace = arcpy.GetParameterAsText(2)
    env.workspace = Routesworkspace
    cases = ['RCs4s3s2c10_S', 'RCs4s3s2c20_S', 'RCs4s3s2c30_S', 'RCs4s3s2c40_S']
    for fc in arcpy.ListFeatureClasses():
        for case in cases:
            if fc.startswith(case):
                fields = ['DISTANCE', 'DURATION']
                SUMDISTANCE = 0
                C = 0
                with arcpy.da.SearchCursor(fc, fields, "FID = 0") as cursor:
                    for row in cursor:
                        DISTANCE = row[0]
                        DURATION = row[1]
                        SUMDISTANCE = SUMDISTANCE + DISTANCE
                        C = C + 1 
                        AVGDISTANCE = SUMDISTANCE / C
    outFile.write('' + str(AVGDISTANCE) + '\n')

Is this the right format for a point txt?

Point
0 34.5 23.2 12
1 65.7 56.5 67
2 34.9 97.9 43
3 67.2 34.3 20
0 Kudos
21 Replies
SepheFox
Frequent Contributor

You can use a Try Except Continue:

try:
  
your script
except:
  
continue  # Ignore the exception and try the next type.

KONPETROV
Occasional Contributor III

This is what i want to do: From several fc which have a field DISTANCE i want to calculate the average of sum of these fields and pass it to a variable AVGDISTANCE.I don't know how to do what you described

0 Kudos