'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)


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 astrochun