0

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!

2
  • Here's a hint for the "low reputation" thing...just sign up for another Stack Overflow site. You'll get a +100 boost to your rep, and can then comment at will. Commented Dec 5, 2013 at 21:59
  • Please note that it is not idiomatic to use docstrings """ as comments in python. You should use actual comments # instead. A docstring explains how to use a function, comments are used to explain how it works. Commented Dec 6, 2013 at 0:23

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.