I have a python script, executing multiple commands using subprocess.call()
. I need to pass data from a gzipped file to one of those commands using stdin, but no matter what I do, the command apparently gets the gzipped data.
This is what I think should work:
import gzip
from subprocess import call
in_fname = 'test.gz'
out_fname = 'test.txt'
gz = gzip.open(in_fname, 'rb')
txt = open(out_fname, 'w')
call(['cat'], stdin=gz, stdout=txt)
But at the end, the 'test.txt'
is compressed and has exactly the same size as the gzipped input file.
If I call gz.read()
then I get the correct decompressed data, as expected. What do I need to do to use the gzipped file as stdin?