piping into a python script

Reedick, Andrew jr9445 at ATT.COM
Thu Jan 24 19:24:32 CET 2008

> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of Donn
> Sent: Thursday, January 24, 2008 12:03 PM
> To: Michał Bentkowski
> Cc: python-list at python.org
> Subject: Re: piping into a python script
> I have tested getopt and it strips the lone '-' out. I can get it from

Try 'foo.py -- -'.  The '--' normally tells the parser to stop parsing args.  Ex: date > -foo.txt; rm -foo.txt; rm -- -foo.txt

I think this will tell you if stdin is being piped in or not:
	import sys
	import os
	print os.isatty(sys.stdin.fileno())

D:\>type a.txt | python a.py

D:\>python a.py

Also if you're lazy, look at the StringIO class:

	if options.filelist is None and len(args) < 1:  # read from stdin
		f = sys.stdin
	elif options.filelist is not None and len(args) < 1:  # read filenames from file
		f = open(options.filelist, 'r')
	elif options.filelist is None and len(args) > 0:  # filenames on command line
		f = StringIO.StringIO('\n'.join(args))
	else:  ## Thanks for playing.

	if f:
		for filename in f:

> -- Segio Aragones (Groo the Wanderer Number 99)

Ah yes, Groo.  Ever wonder who would win if Groo and Forrest Gump fought each other?

More information about the Python-list mailing list