-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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