I have a database with multiple feature classes. Now I extracted a list of feature classes and a list of fields. I figured out how to write the list of feature classes and the corresponding fields to csv. But the csv file gets really long. Instead I would like to write each feature class name with the corresponding field names to a separate csv file. My code looks like this right now:

def csvwriter (csvfile, mylist):
    myfile = open(csvfile,'ab')
    wr = csv.writer(myfile)

datasets = arcpy.ListDatasets()
for ds in datasets:
    dslist = []
    csvwriter (csvfile1,dslist)
    for fc in arcpy.ListFeatureClasses("*","",ds):
        fclist = []
        for field in arcpy.ListFields(fc):
            fieldlist = []
  • You'll need to change the CSV file name to a variable that depends on the feature class, either changing based on an index value (e.g. 1, 2, 3) or based on the feature class name.
    – Erica
    Commented Jul 9, 2014 at 20:30
  • @Erica Thanks, that gave me a push in the right direction. I got it to work.
    – Kat
    Commented Jul 9, 2014 at 20:46
  • OK great -- I was just working up a little script to test it myself, but I'll stop now :)
    – Erica
    Commented Jul 9, 2014 at 20:53

1 Answer 1


Instead of using the same csvfile1 each time, define a variable that depends on the feature class name. For example:

fcList = arcpy.ListFeatureClasses()
for fc in fcList:
    csvFileName = 'CSV_{}.csv'.format(fc) # or just = fc + '.csv'
    for field in arcpy.ListFields(fc):

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.