Created
August 7, 2014 17:18
-
-
Save plathrop/72701e339dc16188616e to your computer and use it in GitHub Desktop.
This file contains 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
def concatenate(self, files, target, compress=False, progress=1): | |
""" | |
Concatenate all the lines from files into the target. | |
If compress is True, compress the file using gzip. Defaults to False. | |
If progress is an integer, log an INFO message every N files (where | |
N is the integer passed). Defaults to 1. | |
Returns target. | |
""" | |
if compress: | |
opener = gzip.open | |
else: | |
opener = open | |
count = len(files) | |
self.logger.debug( | |
'Concatenating %s input files to output file %r', count, target | |
) | |
with contextlib.closing(opener(target, 'w')) as tgt: | |
for line in fileinput.input(files, mode='rb'): | |
fname = fileinput.filename() | |
index = files.index(fname) | |
if fileinput.isfirstline(): | |
self.logger.debug('Concatenating file: %r', fname) | |
if index % progress == 0: | |
self.logger.info('Processed %s/%s files.', | |
index, count) | |
tgt.write(line) | |
return target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment