Python管道到`gzip.open`文件句柄

Python管道到`gzip.open`文件句柄,python,gzip,Python,Gzip,下面的代码段打开一个gzip文件句柄并向其写入一行,然后以追加模式再次打开它,并将子进程的stdout重定向到gzip文件句柄 import gzip import subprocess with gzip.open("./file.txt.gz", "w") as fh: fh.write("this is the first line\n") with gzip.open("./file.txt.gz", "a") as fh: subprocess.call("echo

下面的代码段打开一个gzip文件句柄并向其写入一行,然后以追加模式再次打开它,并将子进程的stdout重定向到gzip文件句柄

import gzip
import subprocess

with gzip.open("./file.txt.gz", "w") as fh:
    fh.write("this is the first line\n")

with gzip.open("./file.txt.gz", "a") as fh:
    subprocess.call("echo this is the second line", shell=True, stdout=fh)
当我试图解压缩文件以查看写入的内容时,会出现以下错误

$ gunzip file.txt.gz
gzip: file.txt.gz: decompression OK, trailing garbage ignored
解压缩的内容仅由第一行组成

$ cat file.txt
this is the first line
当我使用相同的文件句柄来写一行并作为一个进程的输出时,我得到一个文件,它甚至不能被
gunzip
识别

import gzip
import subprocess

with gzip.open("./file.txt.gz", "w") as fh:
    fh.write("this is the first line\n")
    subprocess.call("echo this is the second line", shell=True, stdout=fh)
例如,生成的文件不能是
gunzip
'd

$ gunzip file.txt.gz

gzip: file.txt.gz: not in gzip format

是否有一种方法可以将gzip风格的伪文件句柄传递给通过
子进程运行的进程,或者除了将文件写入未压缩文件,然后返回并压缩它之外,真的没有其他选择吗?

如果搜索StackOverflow,您会发现这个问题偶尔会出现,但答案并不总是很容易实现。它们的要点似乎是
subprocess.call()
不能传递伪文件句柄——它必须是真实的。标准的解决方法似乎是使用
subprocess.Popen()

然而,我找到了一个简单的折衷方案:

import gzip
import subprocess

with gzip.open("file.txt.gz", "wt") as handle:
    handle.write("this is the first line\n")

completed = subprocess.run("echo 'this is the second line'", shell=True, stdout=subprocess.PIPE, universal_newlines=True)

with gzip.open("file.txt.gz", "at") as handle:
    handle.write(completed.stdout)
其想法是延迟附加压缩数据,直到子流程完成:

> gzcat file.txt.gz
this is the first line
this is the second line
> 

Python 3.5中添加了
subprocess.run()
函数。如果搜索StackOverflow,您会发现偶尔会出现此问题,但答案并不总是很容易实现。它们的要点似乎是
subprocess.call()
不能传递伪文件句柄——它必须是真实的。标准的解决方法似乎是使用
subprocess.Popen()

然而,我找到了一个简单的折衷方案:

import gzip
import subprocess

with gzip.open("file.txt.gz", "wt") as handle:
    handle.write("this is the first line\n")

completed = subprocess.run("echo 'this is the second line'", shell=True, stdout=subprocess.PIPE, universal_newlines=True)

with gzip.open("file.txt.gz", "at") as handle:
    handle.write(completed.stdout)
其想法是延迟附加压缩数据,直到子流程完成:

> gzcat file.txt.gz
this is the first line
this is the second line
> 
Python 3.5中添加了
subprocess.run()
函数