Reedick, Andrew
1/24/2008 6:25:00 PM
> -----Original Message-----
> From: python-list-bounces+jr9445=att.com@python.org [mailto:python-
> list-bounces+jr9445=att.com@python.org] On Behalf Of Donn
> Sent: Thursday, January 24, 2008 12:03 PM
> To: Michal Bentkowski
> Cc: python-list@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
False
D:\>python a.py
True
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.
parser.print_help()
exit(1)
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?