3

I am fairly new to python scripting. I am attempting to do some batch conversions from ASCII to Raster in order to save myself some time. However I am continually getting issues with my script.

My current script is:

import arcpy
from arcpy import env
import os
from arcpy.sa import *

# set current workspace
env.workpace = "G:/Arc_GIS/ANUCLIM_DATA/2020-2039/c-ECHAM5/R1/extract"
outworkpace = "G:/Arc_GIS/ANUCLIM_DATA/2020-2039/c-ECHAM5/R1/Python_out"
dataType = "INTEGER"

#Check out the arcGIS spatial Anaylst extension line
arcpy.CheckOutExtension("Spatial")

#Get a list of asciis
for file in arcpy.ListFiles("*.asc"):

    #Print raster list
    print file
    outputraster = file[1:36] + ".tif"
    #save TIF
    #outraster.save(output)
    arcpy.ASCIIToRaster_conversion(file,outputraster,dataType)

I am returning with the error

Traceback (most recent call last):
  File "G:/Arc_GIS/Coding/ASCIIToRaster_conversion", line 15, in <module>
    for file in arcpy.ListFiles("*.asc"):
TypeError: 'NoneType' object is not iterable 

Where am I going wrong?

1
  • 4
    A minor point, but are you sure all your file names are the same length? Using file[1:36] is a bit risky. If you are just swapping .asc for .tif, then using os.path.splitext(file)[0] would be more robust.
    – jon_two
    Commented Jan 12, 2017 at 9:29

1 Answer 1

6
+50

The error is telling you that the output from arcpy.ListFiles("*.asc") is NoneType. This is occuring because you have set your arcpy.env.workpace to a directory that does not exist. The return value of arcpy.ListFiles is a list of strings, if the directory referenced by this function does not exist rather than throw an exception it simply returns None. None cannot be iterated over as the exception states.

For example the script below gives the same error you are receiving:

import os
import arcpy

fake_directory = r'C:\this_path\does_not\exist'
arcpy.env.workspace = fake_directory

for asc_file in arcpy.ListFiles('*.asc'):
    print asc_file

While if you provide a directory that does exist then the same loop will execute without error even if it does not find any files of the type you are looking for. If there are no matches to the wildcard/criteria you provide then arcpy.ListFiles will return an empty list []. Which is iterable but if empty simply won't enter the loop:

real_directory = r'C:\this_path\does\exist'
arcpy.env.workspace = real_directory

for asc_file in arcpy.ListFiles('*.asc'):
    print asc_file

To avoid your script from crashing. Consider checking if the directory exists before using arcpy.ListFiles. Using os.path.isdir, from the standard Python library, should do the trick:

if os.path.isdir(fake_directory):
    for asc_file in arcpy.ListFiles('*.asc'):
        print asc_file
else:
    print '%s is not a valid directory' % fake_directory

The last snippet will only list files in the directory if it actually exists, if it does not exist it will print out a message informing the user of this.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.