Skip to content

Instantly share code, notes, and snippets.

@plathrop
Created August 7, 2014 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plathrop/72701e339dc16188616e to your computer and use it in GitHub Desktop.
Save plathrop/72701e339dc16188616e to your computer and use it in GitHub Desktop.
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