Python EOF error in raw_input ()

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

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

Now I repeat the 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]+"\n") sys.stderr.write("continue Y or N \n") answer = raw_input("Type your answer here") if answer == 'N': sys.exit(1) else: pass` 

This code raises an EOFError. From what I already understand, stdin is reading from cat in this case, and since it has already reached EOF, so raw_input gets EOF here? (I think) The goal is for the script to print a standard error warning and allow me to ignore the warning and continue or close completely. At the end, all output is output and will not contain any error warnings or responses. I saw examples that use try / exception, but I could not figure it out in this context. (For example, why does raw_input not wait for input?)

I think that I can simply attack this problem in no other way and thus create a problem that could be better resolved then jumped over. Any help is appreciated, as always.

+1
source share
3 answers

Yes, the problem is that your raw_input() is read from standard input, which is the result of cat , which is in EOF.

My suggestion would be to exclude cat . It's not obligatory; Python is very good at reading files on its own. Pass the file name on the command line, open it and read it yourself.

 import sys for line in open(sys.argv[1]): # process line 

If you need to process several files, check the fileinput module; it easily handles the reading of several files, as if they were such that cat for you.

+2
source

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 EOF error message):

 import sys for line in sys.stdin: print line sys.stdin = open('CON', 'r') q = raw_input('---->') 

On Unix, you probably just have to replace "CON" with something in the / dev directory.

+3
source

The goal is for the script to display a warning on a standard error and let me choose, ignore the warning and continue or complete completely.

Do you want the selection to come from an online prompt and the data come from a file? So now you are doing something different from the original program: you are reading these two things from different places where they came from the same place before. Therefore, you need to update your design so that it is possible.

Why raw_input is not waiting for input

raw_input waits as long as it takes to get an input string. If standard input is redirected from a file, then the input lines are always available immediately (well, it is limited, for example, by the speed of the hard disk), up to EOF, and at this point it will no longer be available. In short, this is not waiting for you to answer the question for the same reason that it does not wait for you to provide account data: because you are no longer a data source after redirecting from a file.

+1
source

All Articles