【发布时间】:2011-08-21 21:34:33
【问题描述】:
假设我想通过管道将输入输入到 Python 程序,然后在命令行上从用户那里获取输入。
echo http://example.com/image.jpg | python solve_captcha.py
而solve_captcha.py的内容是:
import sys
image_url = sys.stdin.readline()
# Download and open the captcha...
captcha = raw_input("Solve this captcha:")
# do some processing...
以上将触发EOFError: EOF when reading a line 错误。
我还尝试添加sys.stdin.close() 行,它提示ValueError: I/O operation on closed file。
您能否将信息传递给stdin,然后再从用户那里获得输入?
注意:这是一个精简的简化示例 - 请不要回答说“你为什么要在第一种情况下这样做”,这真的很令人沮丧。我只是想知道您是否可以将信息传递给stdin,然后再提示用户输入。
【问题讨论】: