python_reference/useful_scripts/conc_gzip_files.py

34 lines
1.1 KiB
Python
Raw Normal View History

2014-04-01 19:18:34 +00:00
# Sebastian Raschka 03/2014
import gzip
2014-04-01 20:08:26 +00:00
import shutil
2014-04-01 19:18:34 +00:00
import os
2014-04-01 20:08:26 +00:00
#import pyprind
def conc_gzip_files(in_dir, out_file, append=False, print_progress=True):
2014-04-01 19:18:34 +00:00
""" Reads contents from gzipped ASCII or UTF-8 files, decodes them, and
appends the lines to one output file.
Keyword arguments:
in_dir (str): Path of the directory with the gzip-files
out_file (str): Path to the resulting file
append (bool): If true, it appends contents to an exisiting file,
else creates a new output file.
2014-04-01 20:08:26 +00:00
print_progress (bool): prints progress bar if true.
2014-04-01 19:18:34 +00:00
"""
2014-04-01 20:00:20 +00:00
write_mode = 'wb'
2014-04-01 19:40:27 +00:00
gzips = [os.path.join(in_dir, i) for i in os.listdir(in_dir) if i.endswith('.gz')]
2014-04-01 20:08:26 +00:00
#if print_progress:
# pbar = pyprind.ProgBar(len(gzips))
with open(out_file, 'ab' if append else 'wb') as ofile:
2014-04-01 19:18:34 +00:00
for f in gzips:
with gzip.open(f, 'rb') as gzipf:
2014-04-01 20:08:26 +00:00
shutil.copyfileobj(gzipf, ofile)
#if print_progress:
# pbar.update()
2014-04-01 19:18:34 +00:00
if __name__ == '__main__':
conc_gzip_files('/home/usr/my_dir', '/home/usr/test.txt')