Ben*_*evy 2 python zip list attributeerror 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
Run Code Online (Sandbox Code Playgroud)
我收到错误:
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'
Run Code Online (Sandbox Code Playgroud)
这个错误有简单的解决方法吗?
这一行:
zf = zipfile.ZipFile([ dirName + apt + '.zip' ], mode='w')
Run Code Online (Sandbox Code Playgroud)
应该:
zf = zipfile.ZipFile(dirName + apt + '.zip', mode='w')
Run Code Online (Sandbox Code Playgroud)
这是因为ZipFile采用文件名,而不是文件名列表。
| 归档时间: |
|
| 查看次数: |
4036 次 |
| 最近记录: |