Sarah
Sarah

Reputation: 3

"TypeError: 'NoneType' object is not iterable" on what should be a list

I've got some code that I based off of what I found here, but because of my low reputation I can't comment to ask a question. I've also looked through all other posts related to this same error, but, probably due with my lack of familiarity with programming and Python, I haven't found a solution.

Right, so I've got the following code:

import zipfile, os

def dirEntries(dir_name):
    ''' Creates a list of all files in the folder 'dir_name' which we assign when we call the function later'''

    fileList = []
    '''creates an empty list'''
    for file in os.listdir(dir_name):
        '''for all files in the directory given'''
        dirfile = os.path.join(dir_name, file)
        '''creates a full file name including path for each file in the directory'''
        if os.path.isfile(dirfile) and os.path.splitext(dirfile)[1][1:]!='lock':
            '''if the full file name above is a file and it does not end in 'lock' it will be added to the list created above'''
            fileList.append(dirfile)

def makeArchive(fileList, archive, root):
    """
    'fileList' will be a list of file names - full path each name
    'archive' will be the file name for the archive with a full path (ex. "C:\\GIS_Data\\folder.zip")
    """
    a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)

    for f in fileList:
        a.write(f, os.path.relpath(f, root))
        '''I don't completely understand this, but the 'relpath' part seemed integral to not having the entire folder structure saved in the zip file'''
    a.close()

makeArchive(dirEntries(ptdir), ptdir+"pts.zip", ptdir)
makeArchive(dirEntries(polydir), polydir+"polys.zip", polydir)

'ptdir' and 'polydir' are defined in an earlier part of the code.

And I get the following:

Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\GIS_Data\Working\Python\DatabaseExport\ExportScriptTEST.py", line 181, in <module>
    makeArchive(dirEntries(ptdir), ptdir+"pts.zip", ptdir)
  File "C:\GIS_Data\Working\Python\DatabaseExport\ExportScriptTEST.py", line 177, in makeArchive
    for f in fileList:
TypeError: 'NoneType' object is not iterable

I've gone through the process in the interactive window and don't have a problem populating the list when I take apart the functions and feed them in bit by bit, but when I run this all together I get the error. Is the issue that the list is not being populated for some reason, or is there something else going on.

Any assistance would be greatly appreciated!

Upvotes: 0

Views: 3667

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250871

You're not returning anything in this function dirEntries(dir_name), so by default it returns None.

Add a return statement at the end to fix this:

def dirEntries(dir_name):
    ''' Creates a list of all files in the folder 'dir_name' which we assign when we call the function later'''

    fileList = []
    '''creates an empty list'''
    for file in os.listdir(dir_name):
        '''for all files in the directory given'''
        dirfile = os.path.join(dir_name, file)
        '''creates a full file name including path for each file in the directory'''
        if os.path.isfile(dirfile) and os.path.splitext(dirfile)[1][1:]!='lock':
            '''if the full file name above is a file and it does not end in 'lock' it will be added to the list created above'''
            fileList.append(dirfile)
    return fileList

Upvotes: 4

Related Questions