gpt4 book ai didi

python - 将数据传递给 subprocess.check_output

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:27 26 4
gpt4 key购买 nike

我想调用一个脚本,将字符串的内容通过管道传输到它的标准输入并检索它的标准输出。

我不想接触真正的文件系统,所以我不能为它创建真正的临时文件。

使用 subprocess.check_output 我可以得到脚本写的任何东西;我怎样才能将输入字符串输入到它的标准输入中呢?

subprocess.check_output([script_name,"-"],stdin="this is some input")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 672, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "/usr/lib/python2.7/subprocess.py", line 1043, in _get_handles
p2cread = stdin.fileno()
AttributeError: 'str' object has no attribute 'fileno'

最佳答案

使用 Popen.communicate 而不是 subprocess.check_output

from subprocess import Popen, PIPE

p = Popen([script_name, "-"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate("this is some input")

关于python - 将数据传递给 subprocess.check_output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54533698/

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