3

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)
Dharman
  • 30,962
  • 25
  • 85
  • 135
C0ppert0p
  • 634
  • 2
  • 7
  • 23

1 Answers1

0

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.

astrochun
  • 1,642
  • 2
  • 7
  • 18
  • 1
    I was using zlib, and could have sworn I tried zipfile.ZIP_DEFLATED. Thanks for the assist... great link btw. – C0ppert0p Apr 18 '21 at 03:41