1

Using the codes mentioned here Listing feature classes in multiple geodatabases in folder using ArcPy? i was able to list all the feature_classes in multiple geodatabases within a folder. Problem is i don't know how to save the print results to a csv file. I knew, in theory - i have to create a csv file -> open it -> Append the "Print_results" to that file. Since i don't have any experience in python i don't know how to execute it.

Currently i'm trying with this code

import arcpy

dir = r'D:\Test\gdb'
arcpy.env.workspace = dir

gdbList = arcpy.ListWorkspaces('*','FileGDB')

filename = r'D:\Test\FC_List.csv

with open(filename, 'wb') as myfile:
    wr = csv.writer(myfile,quoting=csv.QUOTE_ALL)

for gdb in gdbList:
    arcpy.env.workspace = gdb               
    datasetList = arcpy.ListDatasets('*','Feature')     
    fcList = arcpy.ListFeatureClasses()         
    for fc in fcList:
        print arcpy.env.workspace,fc
    for dataset in datasetList:            
        arcpy.env.workspace = dataset  
        fcList = arcpy.ListFeatureClasses()    
        for fc in fcList:
            print arcpy.env.workspace,fc
        arcpy.env.workspace = gdb
        for each_fs in fcList:
            wr.writerow(each_fs.split(","))
            print (each_fs)

I've refered to the code mentioned here https://www.snip2code.com/Snippet/163571/Write-a-list-of-personal-geodatabase-fea. Actually, this code is creating the Fc_list.csv file. But only as an empty csv file

1

As @dslamb mentioned you need your for loop inside the 'with' statement.

import arcpy
import csv

dir = r'D:\Test\gdb'
arcpy.env.workspace = dir
gdbList = arcpy.ListWorkspaces('*','FileGDB')
filename = r'D:\Test\FC_List.csv'

with open(filename, 'wb') as myfile:
    wr = csv.writer(myfile,quoting=csv.QUOTE_ALL)

    for gdb in gdbList:  # right here and beyond
        arcpy.env.workspace = gdb
        datasetList = arcpy.ListDatasets('*','Feature')
        fcList = arcpy.ListFeatureClasses()
        for fc in fcList:
            wr.writerow(fc.split(",")) # Get fc outside of dataset
            print arcpy.env.workspace,fc
        for dataset in datasetList:
            arcpy.env.workspace = dataset
            fcList = arcpy.ListFeatureClasses()
            for fc in fcList:
                print arcpy.env.workspace,fc
            arcpy.env.workspace = gdb
            for each_fs in fcList:
                wr.writerow(each_fs.split(","))
                print (each_fs)
  • Tried with this code. It is listing the feature_classes in console & creating an empty csv file. – joseph_k Apr 26 '16 at 8:55
  • @saravanaganesh18 what about changing the second to the last line from wr.writerow(each_fs.split(",")) to myfile.write(each_fs + '\n')? I find it strange it is able to print, but not able to write it out to the csv. Both methods have worked for me. – freshhmints Apr 26 '16 at 11:43
  • Still the same. It's printing the feature_classes name and creating the csv file. But an empty one. – joseph_k Apr 26 '16 at 11:50
  • Ok, then that probably means all your feature classes are out of the feature dataset. It would only write out to a csv if it is in a dataset. Put wr.writerow(fc.split(",")) below for fc in fcList: @saravanaganesh18 – freshhmints Apr 26 '16 at 12:05
  • As usual. An empty csv – joseph_k Apr 26 '16 at 12:31
0

This is how I would do this, i am unsure why you are trying to split the fc out if its printing fine, maybe the split is causing the issue. This isn't tested, but give it a shot.

import arcpy
import csv

dir = r'D:\Test\gdb'
arcpy.env.workspace = dir
gdbList = arcpy.ListWorkspaces('*','FileGDB')
filename = r'D:\Test\FC_List.csv'

with open(filename, 'wb') as myfile:
    wr = csv.writer(myfile)
    message = ['featureClass','featureData','gdb']
    wr.writerows([message])

    for gdb in gdbList:
        arcpy.env.workspace = gdb
        # add [''] to include feature classes outside featureDatasets
        fds = arcpy.ListDatasets('*','feature') + ['']

        for featureData in fds:
            fcs = arcpy.ListFeatureClasses('','',featureData)
            for featureClass in fcs:
                print 'Processing feature class: ' + featureClass   
                message = [featureClass,featureData,gdb]
                wr.writerows([message])

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.