Python 使用 zipfile 时出错:'list' 对象没有属性 'tell'

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

我正在尝试使用以下代码仅压缩目录中的 *.csv 文件:

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

我收到错误:

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'

是否有解决此错误的简单方法?

这一行:

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

应该是:

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

这是因为 ZipFile 使用文件名,而不是文件名列表。