Reading From stdin After Command Line Redirection

Ben Finney ben+python at benfinney.id.au
Wed Oct 23 16:46:43 EDT 2013


Tim Daneliuk <tundra at tundraware.com> writes:

> I have a program that runs like this:
>
>   foo.py <inputfile

That's one way to run it. If the user chooses to run it that way, they
have chosen deliberately to run the program non-interactively.

> I want to reconnect stdin to the tty as usual after 'inputfile'
> has been read so that things like raw_input and getpass
> will work as expected.

Why? That's at odds with how the user has already chosen to run the
program. If they wanted to run the program interactively, they wouldn't
have chosen to redirect standard input.

> So, after I do .... = sys.stdin.readlines(), how to I reopen
> stdin in its nonredirected mode?

There would be no point; standard input was never attached to the
console in the first place, it was attached (by the person who chose the
above invocation) to a different file instead. You've already read the
entire file.

Can you speak more about how you intend your program to be used? The
above request is incoherent, and I suspect you've made a design mistake.

-- 
 \       “Philosophy is questions that may never be answered. Religion |
  `\              is answers that may never be questioned.” —anonymous |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list