Skip to content

Instantly share code, notes, and snippets.

@kstreepy
Created June 11, 2019 16:09
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kstreepy/a9800804c21367d5a8bde692318a18f5 to your computer and use it in GitHub Desktop.
Save kstreepy/a9800804c21367d5a8bde692318a18f5 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)
@Gresliebear
Copy link

Thank you for writing this!

@kstreepy
Copy link
Author

Thank you for writing this!

Thanks for letting me know it was helpful. It really made my day that someone found it useful

@NA-Dev
Copy link

NA-Dev commented Oct 11, 2021

Useful for me too, thank you!

@cpreviti
Copy link

cpreviti commented Nov 3, 2021

Thanks very useful!

@ChrKudahl
Copy link

ChrKudahl commented Nov 8, 2021

Thanks, very useful.

Whne using this as a subroutine, changing the working directory could be annoying since it could mess with the function calling it.

@bloosnow
Copy link

very useful. thank you.

@fnOcansey
Copy link

This really helped, thank you.

@dasthagiri-reddy-ynr
Copy link

Thanks for the code

@xiaoguaishoubaobao
Copy link

very useful. thank you.

@adnansarwar06
Copy link

Thanks a lot, saves me a lot of effort

@evanyang22
Copy link

Extremely useful, thanks

@laralds
Copy link

laralds commented Aug 29, 2023

My thanks!!!

@edlo47
Copy link

edlo47 commented Nov 30, 2023

Thank you for posting this! It was useful to me. You've made the world a slightly better place :)

@CraigVarley
Copy link

Very useful, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment