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
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.