Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

file conversion - Error: None-Type error during iteration with Python

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...