Geet Geet - 1 month ago 7
Python Question

NameError while converting tar.gz to zip

I got the following code from my question on how to convert the tar.gz file to zip file.

import tarfile, zipfile
tarf ='sample.tar.gz', mode='r|gz' )
zipf = name='', mode='a', compress_type=ZIP_DEFLATED )
for m in tarf.getmembers():
f = tarf.extractfile( m )
fl =
fn =
zipf.writestr( fn, fl )

but when I run it I get the error.

What should I change in the code to make it work?

NameError: name 'ZIP_DEFLATED' is not defined


ZIP_DEFLATED is a name defined by the zipfile module; reference it from there:

zipf = zipfile.ZipFile(
    '', mode='a',

Note that you don't use the method here; you are not opening members in the archive, you are writing to the object.

Also, the correct ZipFile class signature names the 3rd argument compression. compress_type is only used as an attribute on ZipInfo objects and for the ZipFile.writestr() method. The first argument is not named name either; it's file, but you normally would just pass in the value as a positional argument.

Next, you can't seek in a gzip-compressed tarfile, so you'll have issues accessing members in order if you use tarf.getmembers(). This method has to do a full scan to find all members to build a list, and then you can't go back to read the file data anymore.

Instead, iterate directly over the object, and you'll get member objects in order at a point you can still read the file data too:

for m in tarf:
    f = tarf.extractfile( m )
    fl =
    fn =
    zipf.writestr( fn, fl )