2

我正在尝试使用以下代码仅压缩目录中的 *.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'

这个错误有简单的解决方法吗?

4

1 回答 1

5

这一行:

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

应该:

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

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

于 2016-05-25T21:21:19.680 回答