Python EOF Error in raw_input()
Asked Answered
S

3

2

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]+"\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 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.

Sonatina answered 26/7, 2011 at 16:41 Comment(3)
Sorry about the indents. They were in the original.Sonatina
cat text.txt | ./thescript.py is normally spelled ./thescript.py < text.txt.Jessee
unless he's simplifying that left side, so it doesn't distract us from the problem at hand... ;)Salzburg
G
2

Yes, the problem is that your raw_input() is reading from standard input, which is the output of cat, which is at EOF.

My suggestion would be to eliminate the cat. It is not necessary; Python is perfectly capable of 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 handle multiple files, check out the fileinput module; it easily handles reading multiple files as if they were one, which is what cat does for you.

Gui answered 26/7, 2011 at 16:46 Comment(2)
I thought this was a "walk around" and not a "jump over"! Thanks. Seems sort of obvious now that you said it. Shell is a hard habit to break. :)Sonatina
cat can be a bad habit even in shell. Unless you are actually concatenating files, usually you can save a process by redirecting standard input to a file. e.g. cat foo | grep bar is the same as grep bar <foo except with one fewer process.Gui
S
3

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.

Salzburg answered 26/7, 2011 at 16:59 Comment(0)
G
2

Yes, the problem is that your raw_input() is reading from standard input, which is the output of cat, which is at EOF.

My suggestion would be to eliminate the cat. It is not necessary; Python is perfectly capable of 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 handle multiple files, check out the fileinput module; it easily handles reading multiple files as if they were one, which is what cat does for you.

Gui answered 26/7, 2011 at 16:46 Comment(2)
I thought this was a "walk around" and not a "jump over"! Thanks. Seems sort of obvious now that you said it. Shell is a hard habit to break. :)Sonatina
cat can be a bad habit even in shell. Unless you are actually concatenating files, usually you can save a process by redirecting standard input to a file. e.g. cat foo | grep bar is the same as grep bar <foo except with one fewer process.Gui
J
1

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.

You want the choice to come from an interactive prompt, while the data comes from the file? Well, now you're doing something different from the original program: you're reading those two things from different places, where they came from the same place before. So you need to update your design to allow for that.

Why does the raw_input not wait for input

raw_input waits for as long as is necessary to get a line of input. If the standard input is being redirected from a file, then lines of input are always available immediately (well, limited by e.g. the hard disk speed), up until the EOF, at which point no more will ever be available. In short, it doesn't wait for you to answer the question for the same reason that it doesn't wait for you to supply the invoice data: because you aren't the data source any more once you redirect from the file.

Jessee answered 26/7, 2011 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.