[Tutor] A question about using stdin/out/err vs named files

Ben Finney ben+python at benfinney.id.au
Sun Oct 19 02:46:13 CEST 2014


George R Goffe <grgoffe at yahoo.com.dmarc.invalid> writes:

> When you run a python program, it appears that stdin, stdout, and
> stderr are opened automatically.

That's true of any program on a POSIX-compliant operating system.

> I've been trying to find out how you tell if there's data in stdin
> (like when you pipe data to a python program) rather than in a named
> input file.

What does “there's data in [a stream]” mean here, and how is it distinct
from there being “data in … a named input file”?

The advantage of the standard POSIX streams is that processes can treat
them as very nearly normal files. I don't doubt you have a distinction
you want to detect, but can you be clearer about what that distinction
is?

> It seems like most/all the Unix/Linux commands are able to figure this
> out. Do you know how Python programs do this or might do this?

Hmm. The standard input stream is a separate object from any named input
file you might otherwise open. That's a trivially obvious difference to
detect, so I guess you must not mean that.

Perhaps you mean “is the ‘stdin’ stream connected to an interactive
terminal?”. That's quite a different question from what you seem to be
asking, so I don't know.

-- 
 \       “The best is the enemy of the good.” —Voltaire, _Dictionnaire |
  `\                                                    Philosophique_ |
_o__)                                                                  |
Ben Finney



More information about the Tutor mailing list