问题
I have created a working code to convert GPX files to feature classes in ArcGIS. Unfortunately I have ran into a file that is either corrupted or encrypted (I really don't know). I want to create an exception for these files because there may be more and I do not want these files interrupting the long process. I have tried to create an exception using python's try and except, but now I get the error "TypeError: 'NoneType' object is not iterable" for line 15 which is the FOR loop. A short explanation of my code: First I set up and make the names for the files to be converted. Then they are converted and I have included a counter because I am converting thousands of files. The converted files are put into a list which is then used in the merge to create one big feature class in a geodatabase. For the files that cannot be converted, I want the code to give me the name of the file using arcpy.AddMessage() and move on to the other files. Do you have any ideas? Here is my code:
import arcpy
import datetime
from arcpy import env
arcpy.env.overwriteOutput = True
gpxFolder = 'C:\file\with\GPXs\in\it'
outputGdb = 'C:\the\output\geodatabase'
mergedFile = 'C:the\output\geodatabase\mergedFile'
env.workspace =gpxFolder
def convertGPX2feature(gpxFolder, outputGdb): #, referenceSymbologyLayer, outputLayerFolder
i = 1
fcList = []
for file in arcpy.ListFiles("*.gpx"):
# Convert files from .gpx to feature layer
# First set up the names
inGPX = gpxFolder + "\\" + file
featureName = file.partition(".gpx")[0]
featurename2 = file.partition(".gpx")[1]
fileType = featurename2.split(".")[1]
outfile = outputGdb + "\\" + fileType + "_" + featureName
try:
# Now convert
arcpy.GPXtoFeatures_conversion(inGPX,outfile)
convertedfile = outfile
except:
arcpy.AddMessage("file " + featureName + " failed to convert")
pass
# Add a new field and populate it with the gpx file name to use for the join later
arcpy.AddField_management(convertedfile, "Original_GPX_File", "DOUBLE", 9, "", "", "Original_GPX_File", "NULLABLE", "REQUIRED")
arcpy.CalculateField_management(convertedfile, "Original_GPX_File", featureName)
fcList.append(convertedfile)
# The counter so you know where you are in the iteration
if i%250 == 0:
arcpy.AddMessage(str(i) + " files have been converted at " + str(datetime.datetime.now()))
i += 1
# Merge all of the converted files using fcList as the input
arcpy.AddMessage("And now we merge")
arcpy.Merge_management(fcList, mergedFile )
if __name__ == "__main__":
convertGPX2feature(gpxFolder, outputGdb)
回答1:
So the problem was that arcpy.ListFiles() suddenly stopped working. Once I fixed that (somehow) I ran into the error that the GPX feature already had the new field and could not add another by the same name because the convertedfile object still held the information for the file that last passed when the corrupted file was being iterated over. To fix this, I put the arcpy.AddField() and arcpy.CalculateField() within the try. Now the code works and passes over the corrupted file after giving me the message that it failed to convert using arcpy.AddMessage() and only merges the successfully converted files. The new try and except code looks like this:
outfile = outputGdb + "\\" + fileType + "_" + featureName
try:
# Now convert
arcpy.GPXtoFeatures_conversion(inGPX,outfile)
convertedfile = outfile
# Add a new field and populate it with the gpx file name to use for the join later
arcpy.AddField_management(convertedfile, "Original_GPX_File", "DOUBLE", 9, "", "", "Original_GPX_File", "NULLABLE", "REQUIRED")
arcpy.CalculateField_management(convertedfile, "Original_GPX_File", featureName)
fcList.append(convertedfile)
except:
arcpy.AddMessage("File " + featureName + " could not be converted")
# The counter so you know where you are in the iteration
if i%250 == 0:
arcpy.AddMessage(str(i) + " files have been converted at " + str(datetime.datetime.now()))
i += 1
来源:https://stackoverflow.com/questions/25851028/error-none-type-error-during-iteration-with-python