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

David Rock david at graniteweb.com
Sun Oct 19 15:10:53 CEST 2014


* Peter Otten <__peter__ at web.de> [2014-10-19 10:05]:
> George R Goffe wrote:
> 
> > When you run a python program, it appears that stdin, stdout, and stderr
> > are opened automatically.
> > 
> > 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.
> > 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?
> 
> There is also the fileinput module.

I use fileinput all the time.

"This iterates over the lines of all files listed in sys.argv[1:],
defaulting to sys.stdin if the list is empty. If a filename is '-', it
is also replaced by sys.stdin. To specify an alternative list of
filenames, pass it as the first argument to input(). A single file name
is also allowed."

It gives a fairly clean way to just "do the Right Thing" whether you are
feeding files, or reading from stdin.

-- 
David Rock
david at graniteweb.com


More information about the Tutor mailing list