pythonlistzipattributeerrortell

Python error using zipfile: 'list' object has no attribute 'tell'


I'm trying to zip up only *.csv files in a directory with this code:

allFiles = os.listdir( dirName + apt + '/' )
csvList  = [i for i in allFiles if i.endswith('.csv')]
zf       = zipfile.ZipFile([ dirName + apt + '.zip' ], mode='w')
for f in csvList:
    a    = dirName + apt + '/' + f
    zf.write( a )
#all the elements of a are strings

I get the error:

Traceback (most recent call last):
File "<ipython-input-43-ebf4dc807b56>", line 1, in <module>
zf.write(a)
File "C:\Users\blevy\MCR\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\zipfile.py", line 1347, in write
zinfo.header_offset = self.fp.tell()    # Start of header bytes

AttributeError: 'list' object has no attribute 'tell'

Is there a simple fix to this error?


Solution

  • This line:

    zf = zipfile.ZipFile([ dirName + apt + '.zip' ], mode='w')
    

    should be:

    zf = zipfile.ZipFile(dirName + apt + '.zip', mode='w')
    

    This is because ZipFile takes a file name, not a list of file names.