我想调用一个脚本,将字符串的内容通过管道传输到其标准输入并检索其标准输出。
我不想接触真正的文件系统,所以我无法为其创建真正的临时文件。
using 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'
Use 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")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)