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

asked Apr 25, 2016 at 14:11
2

2 Answers 2

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)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Apr 25, 2016 at 17:22
5
  • Tried with this code. It is listing the feature_classes in console & creating an empty csv file. Commented Apr 26, 2016 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. Commented Apr 26, 2016 at 11:43
  • Still the same. It's printing the feature_classes name and creating the csv file. But an empty one. Commented Apr 26, 2016 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 Commented Apr 26, 2016 at 12:05
  • As usual. An empty csv Commented Apr 26, 2016 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])
answered Jun 19, 2016 at 6:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.