简体   繁体   English

sys.stdin.read()之后的Python raw_input抛出EOFError

[英]Python raw_input following sys.stdin.read() throws EOFError

a similar question has been asked before , but the answers suggested a workaround which is not applicable to my situation. 之前已经提出了类似的问题,但答案提出了一种不适用于我的情况的解决方法。

An email message is piped from mutt to a script, and is read from STDIN: 电子邮件从mutt传送到脚本,并从STDIN读取:

message = sys.stdin.read()
# message is parsed and URLs are printed as a list to choose from...
selected_index = raw_input('Which URL to open?')

I understand that raw_input() will get the EOF left by read(), but is there a way to 'reset' STDIN? 我知道raw_input()会通过read()得到EOF,但有没有办法'重置'STDIN?

Have you tried this: 你试过这个:

message = sys.stdin.read()
sys.stdin = open('/dev/tty')
selected_index = raw_input('Which URL to open?')

This works on Linux; 这适用于Linux; maybe it will work for OSX too. 也许它也适用于OSX。

Try to reset STDIN using sys.stdin.seek(0) 尝试使用sys.stdin.seek(0)重置STDIN

Reference: http://docs.python.org/library/fileinput.html 参考: http//docs.python.org/library/fileinput.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM