Fortunately, gzipped files can be directly concatenated via the cat
CL command, but unfortunately there doesn't seem to be an obvious python command to do this (in the standard library gzip
anyways). However, I only looked briefly. There are probably libraries out there to accomplish this.
Nonetheless, a way to accomplish this using the standard library is to call cat
using subprocess
:
from subprocess import check_call
command = "cat {} {} > {}".format(file1_path, file2_path, output_name)
check_call(command.split()) # Check call takes a list
To generalize this to arbitrary numbers of inputs, you can do:
inputs = ['input1', 'input2', ... 'input9001']
output_name = 'output.gz'
command = "".join(['cat ', '{} ' * len(inputs), '> {out}'])
_call_ = command.format(*inputs, out=output_name).split()
check_call(_call_)
I hope that is helpful to someone.