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

后端 未结 5 737
南方客
南方客 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:18

    ChristopheD's suggestion of using the subprocess module is an appropriate answer to this question. However, it's not clear to me that it will solve your performance problems. You would have to measure the performance of the new code to be sure.

    To convert your sample code:

    import subprocess
    
    p = subprocess.Popen("gzip -c > zipped.gz", shell=True, stdin=subprocess.PIPE)
    p.communicate("Hello World\n")
    

    Since you need to send large amounts of data to the sub-process, you should consider using the stdin attribute of the Popen object. For example:

    import subprocess
    
    p = subprocess.Popen("gzip -c > zipped.gz", shell=True, stdin=subprocess.PIPE)
    p.stdin.write("Some data")
    
    # Write more data here...
    
    p.communicate() # Finish writing data and wait for subprocess to finish
    

    You may also find the discussion at this question helpful.

提交回复
热议问题