我想调用脚本,管道字符串的内容,它的标准输入和检索它的标准输出。将数据传递到subprocess.check_output
我不想碰真实的文件系统,所以我不能为它创造真正的临时文件。
使用subprocess.check_output
我能得到什么脚本编写;我怎样才能得到输入字符串到它的stdin?
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'
'check_output()'的stdin' arg必须是文件对象,而不是字符串。 – jdi 2012-04-11 10:00:21
@jdi显然;所以如何给它像文件一样嘎嘎叫,但不是文件? – Will 2012-04-11 10:06:58
通过选择@larsmans答案:-)如果您不尝试过度使用check_output方便功能并仅执行正常的Popen +通信,则它更容易。否则,你需要手动打开自己的管道,然后将它传递给'check_output(stdin)',然后写入它。 – jdi 2012-04-11 10:09:02