Search code examples
pythonzipunzipgzip

Unzip gz files within folders in a main folder using python


I have .gz zipped files within multiple folders that are all within a main folder called "usa". I was able to extract an individual file using the code below.

import gzip
import shutil
source=r"C:\usauc300.dbf.gz"
output=r"C:\usauc300.dbf"
with gzip.open(source,"rb") as f_in, open(output,"wb") as f_out:
    shutil.copyfileobj(f_in, f_out)

I have searched high and low but can't find an equivalent to the command line option gzip -dr..... which means "decompress recursive" and will go through each folder and extract the contents to the same location while deleting the original zipped file. Does anyone know how I can use python to loop through folders within a folder, find any zipped files and unzip them to the same location while replacing the unzipped file with the zipped one?


Solution

  • I believe that's because gzip never operates over directories, it acts as a compression algorithm unlike zip and tar where we could compress directories. python's implementation of gzip is to operate on files. However recursive traversal of a directory tree is easy if we look at the os.walk call.

    (I haven't tested this)

    def gunzip(file_path,output_path):
        with gzip.open(file_path,"rb") as f_in, open(output_path,"wb") as f_out:
            shutil.copyfileobj(f_in, f_out)
    
    def recurse_and_gunzip(root):
        walker = os.walk(root)
        for root,dirs,files in walker:
            for f in files:
                if fnmatch.fnmatch(f,"*.gz"):
                    gunzip(f,f.replace(".gz",""))