[GIS] Listing all Feature datasets and feature classes from single Geodatabase into CSV

arcpycsvesri-geodatabaseindentationerrorsave

I have prepared a Python code and I got the following errors:

"Parsing error IndentationError: expected an indented block (line 11)"

and

"Runtime error Traceback (most recent call last): File "",
line 10, in TypeError: 'NoneType' object is not iterable"

The purpose of the code is to list all feature datasets & feature classes from a single geodatabase into CSV.

import os
import csv
import arcpy
from arcpy import env

env.workspace = "I:\J_Prashant's External HD\High Speed Rail\Task from Greg Campbell\Layers\HST_BP_GIS_Data.gdb"

datasetList = arcpy.ListDatasets('*','Feature')

for dataset in datasetList:
    arcpy.env.workspace = dataset
    fcList = arcpy.ListFeatureClasses()
    for fc in fcList:
        print arcpy.env.workspace,fc

csv_out = open('I:\\python\\List-Feature-Class\\test2.csv', 'wb')
mywriter = csv.writer(csv_out)
rows = zip(arcpy.env.workspace,fc)
mywriter.writerows(rows)
csv_out.close()

Best Answer

I think you should research/ask about writing to a CSV file separately at StackOverflow because the technique you are using to do that is pure Python rather than ArcPy.

For the remainder I think this should work (use a modification of your file geodatabase location where I have used C:\temp\test.gdb):

import arcpy

arcpy.env.workspace = r"C:\temp\test.gdb"

datasetList = arcpy.ListDatasets('*','Feature')

for dataset in datasetList:
    # arcpy.env.workspace = dataset
    arcpy.env.workspace = r"C:\temp\test.gdb\{0}".format(dataset)
    fcList = arcpy.ListFeatureClasses()
    for fc in fcList:
        print arcpy.env.workspace,fc

The two main issues were:

  1. using back slashes in your pathname - you need to convert them to double backslashes, change them to forward slashes or simply escape them using the letter r like I have above
  2. the single quote in your pathname will leave Python looking for the other one to close that string