File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Sebastian Raschka 03/2014
2+
3+ import gzip
4+ import os
5+
6+ def conc_gzip_files (in_dir , out_file , append = False ):
7+ """ Reads contents from gzipped ASCII or UTF-8 files, decodes them, and
8+ appends the lines to one output file.
9+
10+ Keyword arguments:
11+ in_dir (str): Path of the directory with the gzip-files
12+ out_file (str): Path to the resulting file
13+ append (bool): If true, it appends contents to an exisiting file,
14+ else creates a new output file.
15+
16+ """
17+ write_mode = 'w'
18+ if append :
19+ write_mode = 'a'
20+ gzips = [os .path .join (in_dir , i ) for i in os .listdir (in_dir ) if i .endswith (i )]
21+ with open (out_file , write_mode ) as ofile :
22+ for f in gzips :
23+ with gzip .open (f , 'rb' ) as gzipf :
24+ for line in gzipf :
25+ ofile .write (line .decode ())
26+
27+ if __name__ == '__main__' :
28+ conc_gzip_files ('/home/usr/my_dir' , '/home/usr/test.txt' )
You can’t perform that action at this time.
0 commit comments