11# Sebastian Raschka 03/2014
22
33import gzip
4+ import shutil
45import os
56
6- def conc_gzip_files (in_dir , out_file , append = False ):
7+ #import pyprind
8+
9+ def conc_gzip_files (in_dir , out_file , append = False , print_progress = True ):
710 """ Reads contents from gzipped ASCII or UTF-8 files, decodes them, and
811 appends the lines to one output file.
912
@@ -12,17 +15,19 @@ def conc_gzip_files(in_dir, out_file, append=False):
1215 out_file (str): Path to the resulting file
1316 append (bool): If true, it appends contents to an exisiting file,
1417 else creates a new output file.
18+ print_progress (bool): prints progress bar if true.
1519
1620 """
1721 write_mode = 'wb'
18- if append :
19- write_mode = 'ab'
2022 gzips = [os .path .join (in_dir , i ) for i in os .listdir (in_dir ) if i .endswith ('.gz' )]
21- with open (out_file , write_mode ) as ofile :
23+ #if print_progress:
24+ # pbar = pyprind.ProgBar(len(gzips))
25+ with open (out_file , 'ab' if append else 'wb' ) as ofile :
2226 for f in gzips :
2327 with gzip .open (f , 'rb' ) as gzipf :
24- for line in gzipf :
25- ofile .write (line )
28+ shutil .copyfileobj (gzipf , ofile )
29+ #if print_progress:
30+ # pbar.update()
2631
2732if __name__ == '__main__' :
2833 conc_gzip_files ('/home/usr/my_dir' , '/home/usr/test.txt' )
0 commit comments