11

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()
5
  • 2
    Try putting your gdb in a directory with no spaces in the filnames (e.g. I:\GIS\HST_BP_GIS_Data.gdb) and see if that works. Some ArcPy tools can't handle spaces.
    – Erica
    Commented Sep 16, 2014 at 17:53
  • 3
    I would also add never use special characters such as & - ' in your folder name as these will upset your code.
    – Hornbydd
    Commented Sep 16, 2014 at 19:13
  • 3
    use 'r' in front of your env.workspace name or use double back-slashes the way you do with your csv_out variable to negate the escape character. It sounds like your datasetList object is still null because it isn't reading it properly from the workspace.
    – Sleep6
    Commented Sep 16, 2014 at 19:20
  • didn't work though...I am getting the same error
    – user37082
    Commented Sep 16, 2014 at 23:26
  • Try this in shell, see if it returns false - os.path.isdir(yourworkspace) If you should be able to run something as simple as the example shown here - resources.arcgis.com/en/help/main/10.1/index.html#//… Try it and see.
    – Sleep6
    Commented Sep 17, 2014 at 17:21

4 Answers 4

15

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
3
  • This is not working for me. Lists a few feature classes then dies with "TypeError: 'NoneType' object is not iterable" error. In fact i didn't find any script that works and simply spits out a list of feature classes. Either errors out or doesn't run at all (ESRI examples). Was hoping for paste and run solution while substituting a path to my gdb. Any ideas? Commented May 9, 2018 at 15:54
  • @JakubSisakGeoGraphics I think it needs a new question that says you are trying to implement this answer and gives details about the data items that work and those that fall over.
    – PolyGeo
    Commented May 9, 2018 at 20:05
  • 1
    I Faced the same NoneType problem like Jakub Sisak GeoGraphics. There is a missing reference to the GDB. arcpy.List_Datasets() is only returning the name of the dataset not the full path. The following worked for me: for dataset in datasetList: arcpy.env.workspace = "C:/temp/test.gdb/"+dataset
    – EikeMike
    Commented Nov 9, 2018 at 9:37
2
import arcpy
out_csv = r'C:\GIS\temp\features.csv'
arcpy.env.workspace = r'C:\GIS\data\Bakgrundskartor_LMV\Oversiktskartan.gdb'

with open(out_csv, 'a') as csvfile: #Open the file in append mode
    for f in arcpy.ListFeatureClasses():
        csvfile.write(f+'\n') #For each feature class, write name to the file, ending with a new line

enter image description here

1

Code sample revisited below. Just move the arcpy.ListFeatureClasses out of the look and the code will run successfully.

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

for dataset in datasetList:
    arcpy.env.workspace = dataset

    for fc in fcList:
        print arcpy.env.workspace,fc
0
-1

Try to add the path to the name of each dataset when you establish the env.workspace value:

import csv, arcpy, os
from arcpy import env

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

env.workspace = path_

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

for dataset in datasetList:

arcpy.env.workspace = os.path.join(path_,dataset)
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
    print arcpy.env.workspace,fc
1
  • This code looks like it would give an indentation error on the line that sets arcpy.env.workspace, and it seems like you have added an extra line of code and another function, making it less pythonic.
    – PolyGeo
    Commented Nov 14, 2015 at 6:26