Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

input - Python EOF Error in raw_input()

I am trying to get input from the user at the command prompt. The program reads in data from a text file in the manner of "cat text.txt | ./thescript.py"

At the point of the script in question, all data has already been read in, processed, and put into a list of lists.

Now I am iterating through that list of lists looking for questionable items. The code basically looks like this:

for invoice in parsedlist:
      if invoice[-1] == 3:
          sys.stderr.write("triple duplicate at " + invoice[2]+' : ' + invoice[3]+"
")
          sys.stderr.write("continue Y or N 
")
          answer = raw_input("Type your answer here")
          if answer == 'N':
              sys.exit(1)
          else: 
              pass`

This code results in an EOFError. From what I already understand, stdin is the read from cat in this case and since it has already reached EOF that is why raw_input gets EOF here? (I think) The objective is to have the script print a warning to standard error and let me choose whether to ignore the warning and continue or quit entirely. In the end all output goes to std out and will not include any of the error warnings or responses. I have seen examples that use a try/exception but I have not been able to make sense of it in this context. (Eg. Why does the raw_input not wait for input?)

I think I may just be attacking this problem in the wrong way and thus creating a problem that could be better walked around then jumped over. Any help is appreciated as always.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This works under windows (I tested it by running python cons.py < cons.py and was able to see the prompt and not get an error about EOF):

import sys

for line in sys.stdin:
    print line

sys.stdin = open('CON', 'r')
q = raw_input('---->')

Under Unix, you'd probably just have to replace 'CON' with something in the /dev dir.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...