简体   繁体   中英

Python Zipfile module is throwing errors

I'm trying to get a python Zip module to compress data.

But all it's doing is throwing an error:

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

I've tried using

ZipFile.ZIP_DEFLATED , and

zipfile.ZipFile.ZIP_DEFLATED , but get the exact same result.

If I remove the compression and compression level parameters, the code runs just fine, but the whole point of using it is to compress the data.

Any help would be greatly appreciated.

Here is the code.

    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)

Fairly sure it's zipfile.ZIP_DEFLATED. Note the the docs mention the need for zlib dependency for ZIP_DEFLATED. You might find these examples here useful: https://pymotw.com/2/zipfile/#creating-new-archives .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM