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:filewithGPXsinit'
outputGdb = 'C: heoutputgeodatabase'
mergedFile = 'C:theoutputgeodatabasemergedFile'
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)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…