pythons3上传zip文件时会出现AttributeError:“ZipFile”对象没有属性“tell”

2024-06-26 14:55:35 发布

您现在位置:Python中文网/ 问答频道 /正文

I try to upload a zip file to my S3 bucket, but getting 

AttributeError: 'ZipFile' object has no attribute 'tell' error

^{pr2}$

这里怎么了?(zipf是我的zipfile)

如果我像这样修改代码

   with open(zipf) as f:
            k.send_file(f)

我明白了

TypeError: coercing to Unicode: need string or buffer, ZipFile found

我创建的zip文件如下

zipfilename = 'AAA_' + str(datetime.datetime.utcnow().replace(microsecond=0)) + '.zip'
    zipf = zipfile.ZipFile(zipfilename, mode='w')

    for root, dirs, files in os.walk(path):
        for f in files:
            zipf.write(os.path.join(root, f))
    zipf.close()

Tags: topathinfordatetimeosrootfiles
1条回答
网友
1楼 · 发布于 2024-06-26 14:55:35

您的zipfile是用zipf = zipfile.ZipFile(zipfilename, mode='w')创建的。在

zipfile documentation之后,tell()属性只为mode='r'定义。在

没有理由使用zipfile对象,而且在写模式下,上载我们想要读取的文件(无论是zip文件还是其他文件),以便将其上载到S3。在

只需在调用键上的open(zipfilename, 'r')之前使用open(zipfilename, 'r')。在

相关问题 更多 >