Unzip gz files within folders in a main folder using python

别等时光非礼了梦想. 提交于 2021-02-07 11:01:10

问题


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?


回答1:


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",""))


来源:https://stackoverflow.com/questions/45596742/unzip-gz-files-within-folders-in-a-main-folder-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!