Python equivalent of piping file output to gzip in Perl using a pipe

后端 未结 5 751
南方客
南方客 2021-02-14 02:13

I need to figure out how to write file output to a compressed file in Python, similar to the two-liner below:

open ZIPPED, \"| gzip -c > zipped.gz\";
print ZI         


        
5条回答
  •  伪装坚强ぢ
    2021-02-14 02:36

    Try something like this:

    from subprocess import Popen, PIPE
    f = open('zipped.gz', 'w')
    pipe = Popen('gzip', stdin=PIPE, stdout=f)
    pipe.communicate('Hello world\n')
    f.close()
    

提交回复
热议问题