import subprocess
p = subprocess.Popen('gzip -c > zipped.gz', shell=True, stdin=subprocess.PIPE)
p.stdin.write('Some data')
p.communicate()
Python equivalent of piping file output to gzip in Perl using a pipe