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
-
2gis.stackexchange.com/questions/48537/…Alex Tereshenkov– Alex Tereshenkov2016年04月25日 14:18:30 +00:00Commented Apr 25, 2016 at 14:18
-
I believe you need the for loop inside the with statement.dslamb– dslamb2016年04月25日 15:59:46 +00:00Commented Apr 25, 2016 at 15:59
2 Answers 2
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– joseph_k2016年04月26日 08:55:09 +00:00Commented Apr 26, 2016 at 8:55
-
@saravanaganesh18 what about changing the second to the last line from
wr.writerow(each_fs.split(","))
tomyfile.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– freshhmints2016年04月26日 11:43:53 +00:00Commented 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.joseph_k– joseph_k2016年04月26日 11:50:21 +00:00Commented 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(","))
belowfor fc in fcList:
@saravanaganesh18freshhmints– freshhmints2016年04月26日 12:05:30 +00:00Commented Apr 26, 2016 at 12:05 -
As usual. An empty csvjoseph_k– joseph_k2016年04月26日 12:31:32 +00:00Commented Apr 26, 2016 at 12:31
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])