Skip to content

Instantly share code, notes, and snippets.

@extratone
Forked from kstreepy/gz_extract.py
Created September 25, 2022 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save extratone/273f05e15256411781b21043773c3cc8 to your computer and use it in GitHub Desktop.
Save extratone/273f05e15256411781b21043773c3cc8 to your computer and use it in GitHub Desktop.
For a given directory, unzip all .gz files in folder, save unzipped files in folder and deleted zipped files. A python solution for instances where you do not have access to PowerShell.
import os, gzip, shutil
dir_name = 'x'
def gz_extract(directory):
extension = ".gz"
os.chdir(directory)
for item in os.listdir(directory): # loop through items in dir
if item.endswith(extension): # check for ".gz" extension
gz_name = os.path.abspath(item) # get full path of files
file_name = (os.path.basename(gz_name)).rsplit('.',1)[0] #get file name for file within
with gzip.open(gz_name,"rb") as f_in, open(file_name,"wb") as f_out:
shutil.copyfileobj(f_in, f_out)
os.remove(gz_name) # delete zipped file
gz_extract(dir_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment