arcpy write a list of feature datasets from a geodatabase to a csv file

Lets be a little more specific as to what we want to do so I will reformulate the title now:

How to write to a CSV file a list of ESRI ArcGIS personal geodatabase feature classes and feature sets using arcpy?

As usual here is the code first since we are all impatient :)

code is on Github
import csv, arcpy
from arcpy import env
# set path using raw string to personal geodatabase
env.workspace = r"c:\users\mdiener\documents\mypersonalgeodatabase.gdb"
# create a python list of all datasets in personal geodatabase
datasetList = arcpy.ListDatasets('*','Feature')
# open a file for writing.
filename = r'c:\users\mdiener\documents\demo.csv'
#open create new csv file
with open(filename, 'wb') as myfile:
wr = csv.writer(myfile,quoting=csv.QUOTE_ALL)
for dataset in datasetList:
env.workspace = dataset
fcList = arcpy.ListFeatureClasses()
print(fcList)
# uncomment this if you want the csv all in one line
# if not use code as is
# wr.writerow(fcList)
for each_fs in fcList:
wr.writerow(each_fs.split(","))
print (each_fs)
view raw gdbList2csv.py hosted with ❤ by GitHub
most of it is self explanatory but who knows.  We assume you have a running copy of ArcGIS 10.2 in this case with on my machine I have ArcGIS10.2 installed and the python interpreter is located here c:\arcgis\Python27\ArcGIS10.2\python.exe

I think only a few lines might need explaining

line 27  wr.writerow(each_fs.split(","))

The .split(",") prevents our CSV from spitting out "f", "c", "_","n","a","m","e", "F","o","o.......
  where our list is  fcList= ["fc_nameFoo","fc_nameFee"]

If you want to write this all to one single line in the csv you can uncomment the section in the first for loop located at line25

Other wise the script will output every feature class name to one line in the new CSV file.

Hope that helps.

cheers
Michael

Comments