1

i am looking for a method by which i can export fc list and fc count into xls or csv. for example i have 3 featureclass in Scratch.gdb.

1.PaLakh

2.PATest1

3.Point

i want all 3 of these featureclass name to be exported into a csv or xls. also the feature count of every featureclass against featureclass name.

i tried the below mentioned code, but instead of exporting fclist it exports every character of fc into a new row like.

p

o

i

n

t

import csv, arcpy, os
 from arcpy import env
 env.workspace = r"C:\Akhil_Office_1\Python\ToolBox\Scratch\Scratch.gdb"
 fclist = arcpy.ListFeatureClasses()
 for fc in fclist:
  csv_out = open(r'C:\Akhil_Office_1\Python\ToolBox\Scratch\test.csv', 'wb')
  mywriter = csv.writer(csv_out)
  rows = zip(arcpy.env.workspace, fc)
  mywriter.writerows(rows)
  csv_out.close()

or is there any other way in arcpy to export feature class name list into xls ?

2
  • Your code creates a variable called fclist which should contain a list of feature classes. However, it does not use that variable anywhere. The output you provided does not appear to match your code snippet. For us to help we need to see exactly the code snippet that you are running to produce the symptoms you observe. My strong suspicion is that the cause of your observations stems from how you are iterating your list which is pure Python rather than ArcPy and may be better researched at Stack Overflow.
    – PolyGeo
    Commented Sep 17, 2015 at 7:02
  • i corrected coding and re framed question to explain it clearly Commented Sep 17, 2015 at 10:22

2 Answers 2

2

I'm not familiar with the csv module, but you can use the open command to create a csv file just fine. Open, the file, then iterate through your feature classes. Get the count, create a string with a comma separating the name and the count, and then write it to your file. Finally, when you're all done, close the file.

Something like this (untested):

import arcpy

arcpy.env.workspace = r"C:\Akhil_Office_1\Python\ToolBox\Scratch\Scratch.gdb"
csv_out = open(r'C:\Akhil_Office_1\Python\ToolBox\Scratch\test.csv', 'w')

#list feature classes
fclist = arcpy.ListFeatureClasses()

#iterate feature classes
for fc in fclist:

    #Get feature count
    fcCount = arcpy.GetCount_management (fc).getOutput (0)

    #create string to add to csv
    outStr = "{0},{1}\n".format (fc, fcCount)

    #write to file
    csv_out.write (outStr)

#Close csv file
csv_out.close ()
2
  • 1
    +1 Nice, simple solution. The excellent script comments will certainly help future readers.
    – Aaron
    Commented Sep 17, 2015 at 23:32
  • thanks Emil your code run flawless. but when i try to open the created csv file. it is always locked by another user. or opens in read only. any idea why this is happening ? Commented Sep 18, 2015 at 5:23
1

i improved the Script and appended the arcpy.da.walk method in it, so it could search in subfolders

still one problem persist, if any shapefile contains space in its name the script fails. otherwise it is working fine.

import arcpy
import os
#taking input from user
arcpy.env.workspace = arcpy.GetParameterAsText(0)
OutFolder = arcpy.GetParameterAsText(1)

outStr = "{0}{1}".format(OutFolder, "\\FcCount.csv")

csv_out = open(outStr, 'w')

#list feature classes with arcpy.da.walk method wchich walk through every subfolder


feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(arcpy.env.workspace, datatype="FeatureClass"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))
        print feature_classes

#iterate feature classes and create csv
for fc in feature_classes:

    #Get feature count
    fcCount = arcpy.GetCount_management (fc).getOutput (0)

    #create string to add to csv
    outStr = "{0},{1}\n".format (fc, fcCount)

    #write to file
    csv_out.write (outStr)

#Close csv file
csv_out.close ()

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.