Problems reading fra sys.stdin and later on duing raw_input 
Author Message
 Problems reading fra sys.stdin and later on duing raw_input

Hey

I want to read a file from sys.stdin and then later on do a
raw_input. But I get an error because sys.stdin is closed after the
file is read.

Here is an example that illustrates the problem.

/tmp/x.py:
        #!/usr/bin/env python

        import sys

        lines = sys.stdin.readlines()
        for line in lines:
            print line

        a = raw_input('Prompt: ')
        print a


test
Prompt: Traceback (innermost last):
  File "/tmp/x.py", line 9, in ?
    a = raw_input('Prompt: ')
EOFError: EOF when reading a line

Is there *any* way I can solve this problem?

Btw, I use python-1.5.2 on Linux 2 and Solaris 7

Regards,
Peter Kristensen



Tue, 03 Sep 2002 03:00:00 GMT  
 Problems reading fra sys.stdin and later on duing raw_input

Quote:
> I want to read a file from sys.stdin and then later on do a
> raw_input. But I get an error because sys.stdin is closed after the
> file is read.

> Is there *any* way I can solve this problem?

Maybe you could use ncurses to read from the keyboard.  I've never used
it, but I'd be suprised if it goes through stdin.  I tried writing
something, but it segfaults for some reason:

#!/usr/bin/env python

import curses
from pdb import set_trace
st = set_trace

def get_line (prompt):
    screen = curses.initscr ()
    # screen.cbreak () # screen has neither of these attributes...
    # screen.noecho ()
    response = screen.getstr (0,0)
    screen.endwin ()
    return response

if __name__ == '__main__':
    import sys
    std_input = sys.stdin.readlines ()
    print 'This is what was piped:'
    # print std_input
    keyboard_input = get_line ('Enter something else: ')
    print 'This is what was typed:'
    print keyboard_input

This was based on the tutorial at http://python.org/doc/howto/curses/

Alex.



Tue, 03 Sep 2002 03:00:00 GMT  
 Problems reading fra sys.stdin and later on duing raw_input

Quote:

> I want to read a file from sys.stdin and then later on do a
> raw_input. But I get an error because sys.stdin is closed after the
> file is read.

[...]

stdin is still open, but it's connected to the output of echo, not the
terminal.  This is a limitation of any Unix program run in a pipeline
this way.

Some solutions I can see are:

 * Put the data in a file and pass the name of the file to your
   script, then open that file:

     lines = open(sys.argv[1]).readlines()

   This is probably the nicest solution, and the least prone to making
   wierd things happen if your script gets run differently.

 * Open /dev/tty to replace sys.stdin:

     sys.stdin = open('/dev/tty')
     a = raw_input('Prompt: ')

 * Redirect stdin to another file handle when you run your script, and
   read from that:

     sys.stdin = os.fdopen(3)
     a = raw_input('Prompt: ')

   $ (echo -n test | ./x.py) 3<&0

--
         Carey Evans  http://home.clear.net.nz/pages/c.evans/

"*South Park* is another movie straight from the smoking pits of Hell."
                       - http://www.capalert.com/capreports/southpark.htm



Thu, 05 Sep 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. using sys.stdin.raw_input and sys.stdin.readline together in a platform independent way

2. non-blocking sys.stdin.read() on win32?

3. sys.stdin.read() HELP!

4. Error using sys.stdin.read()

5. can i make sys.stdin.read() not wait?

6. Terminal reads and sys.stdin

7. Chosing between sys.argv and sys.stdin

8. banging head against sys.stdin and tkinter on win32

9. sys.stdin.readlines() in 2.2a ?

10. reusing sys.stdin?

11. how to calculate the size of sys.stdin?

12. sys.stdin.readline

 

 
Powered by phpBB® Forum Software