Reading From stdin After Command Line Redirection

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Oct 24 07:33:44 EDT 2013


On 24 October 2013 01:09, Tim Daneliuk <tundra at tundraware.com> wrote:
>
> Now that I think about it, as I recall from the prehistoric era of writing
> lots of assembler and C, if you use shell redirection, stdin shows
> up as a handle to the file

Yes this is true. A demonstration using seek (on Windows but it is the
same in this sense):

$ cat test.py
import sys
sys.stdin.seek(0)
print('Seeked fine without errors')
$ python test.py
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    sys.stdin.seek(0)
IOError: [Errno 9] Bad file descriptor
$ python test.py < other.dat
Seeked fine without errors

> and there is no way to retrieve/reset it
> its default association with the tty/pty.  Since python is layered on
> top of this, I expect the same would be the case here as well.

I think it is true that you cannot restore the association but you can
just open the tty explicitly as described here:
http://superuser.com/questions/569432/why-can-i-see-password-prompts-through-redirecting-output

(I can't test that right now as it obviously doesn't work on Windows).


Oscar



More information about the Python-list mailing list