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

问题描述 投票:0回答:1

目前,我有代码解压缩文件,然后使用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', 'rb') as in_file:
    g_unzip_process = Popen(['gunzip', '-c'], stdin=in_file, stdout=PIPE)

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

with open('./outfile.gz', 'wb+') as out_file:
    Popen(['gzip'], 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中?

[目前,我有代码解压缩文件,然后使用open('./ file.gz','rb')作为in_file的Popen将其压缩回另一个文件:g_unzip_process = Popen(['gunzip' ,'-c'],stdin = in_file,...

python io pipe gzip popen
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.