如何将python程序的标准输出输入Popen标准输入?

目前,我有解压缩文件的代码,然后使用Popen将其压缩回另一个文件中

with open('./file.gz','rb') as in_file:
    g_unzip_process = Popen(['gunzip','-c'],stdin=in_file,stdout=PIPE)

with open('./outfile.gz','wb+') as out_file:
    Popen(['gzip'],stdin=g_unzip_process.stdout,stdout=out_file)

现在,我正在尝试在两者之间插入一些东西。

它应该像这样:

with open('./file.gz',stdout=PIPE)

transform_process = Popen(['python','transform.py'],stdin=transform_process.stdout,stdout=out_file)

我的transform.py代码看起来像这样

for line in sys.stdin:
    sys.stdout.write(line)
sys.stdin.close()
sys.stdout.close()

运行后,为什么我的outfile.gz文件为空? 我还尝试通过运行以下内容来阅读每一行:

for line in transform_process.stdout:
    print(line)

我如何做到使这些行写在outfile.gz中?

zhaozihong1 回答:如何将python程序的标准输出输入Popen标准输入?

我能够通过调用Popen.wait()写入outfile.gz来使其工作

本文链接:https://www.f2er.com/3043880.html

大家都在问