gpt4 book ai didi

python - gzip.open 作为 subprocess.Popen 的标准输入

转载 作者:行者123 更新时间:2023-11-28 22:27:53 27 4
gpt4 key购买 nike

我正在尝试使用 subprocess.Popen 启动一个 python 程序,stdin 是一些常规文本文件,就像这样。

subprocess.Popen(args, stdout=stdoutFile, stderr=stderrFile, stdin=open(TEXT_FILE))

而且效果很好。但是,如果我尝试打开一个 gzip 文件,我的过程就会失败。

import gzip
subprocess.Popen(args, stdout=stdoutFile, stderr=stderFile, stdin=gzip.open(GZFILE))

我不确定为什么。我的过程认为它没有得到任何数据。

知道为什么吗?两者不应该可以互换吗?

最佳答案

如果你传递gzip.open(..)的返回值,它的内部文件描述符被传递给文件;从子进程读取它会返回原始数据,而不是解压后的数据。

您需要将解压后的数据传递给子进程的标准输入:

subprocess.Popen(args, stdout=stdoutFile, stderr=stderFile,, stdin=subprocess.PIPE)
with gzip.open(GZFILE) as f:
shutil.copyfileobj(f, p.stdin)
# p.stdin.close() # to denote the end of the file.

关于python - gzip.open 作为 subprocess.Popen 的标准输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43896226/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com