Recently, I had a lot of CSV files that needed uncompressing.
I didn’t like the options available, because I didn’t fully understand them.
I created an alternative solution with my favourite programming language: Python.
In this tutorial, I will be using Python along with the
If you haven’t, you will need to install Python.
Uncompress All GZip Files in a Directory
from glob import glob import os path = '/path/to/' # Show location of the files list_of_files = glob(path + '*.gz') # list gzip files bash_command = 'gzip -dk ' + ' '.join(list_of_files) # create bash command os.system(bash_command) # Run command in Terminal
Understand the Python Script
In the command above,
glob is used to list all the files that end up with the
It returns a list:
The Gunzip Command
join(), I convert the list to a string.
bash_command variable is created to reproduce a gunzip command.
It is a string that uses the
gzip -dk file.gz format.
-dk is used to make sure that with uncompress, but also keep the compressed version.
This is what is stored in the
# gzip -dk file.gz /path/to/file1.csv.gz /path/to/file2.csv.gz
Run the Command in The Terminal With os
os.system() command, we will execute the
gunzip command to uncompress the files.
This is it. You now have uncompressed multiple gzip files at once using Python and gunzip.
SEO Strategist at Tripadvisor, ex- Seek (Melbourne, Australia). Specialized in technical SEO. In a quest to programmatic SEO for large organizations through the use of Python, R and machine learning.