Python Zipfile 模块抛出错误

Python Zipfile module is throwing errors

我正在尝试获取 python Zip 模块来压缩数据。

但它所做的只是抛出一个错误:

with ZipFile(O_file7,mode='w',compression=ZipFile.ZIP_DEFLATED,compressionlevel=9) as file:
AttributeError: type object 'ZipFile' has no attribute 'ZIP_DEFLATED'

我试过使用

ZipFile.ZIP_DEFLATED,以及

zipfile.ZipFile.ZIP_DEFLATED,但得到完全相同的结果。

如果我删除压缩和压缩级别参数,代码运行得很好,但使用它的全部意义在于压缩数据。

如有任何帮助,我们将不胜感激。

这是代码。

    import zlib
    from zipfile import ZipFile
    HOME2 = "h:\\passport\tran-14d - ollw mass print\"
    ARCHIVE = HOME2+"\Archive\"
    I_file1 = "masterfile.txt"             
    O_file7 = "New.zip" 
    def ZIPPER():
        os.chdir(HOME2)
        with ZipFile(O_file7,mode='w',compression=ZipFile.ZIP_DEFLATED,compressionlevel=9) as file:
            os.chdir(HOME2)
            file.write(I_file1)

很确定是 zipfile.ZIP_DEFLATED。请注意,文档提到 ZIP_DEFLATED 需要 zlib 依赖项。您可能会发现这里的这些示例很有用:https://pymotw.com/2/zipfile/#creating-new-archives.