stdin or optional fileinput
the.theorist at gmail.com
Wed Mar 15 17:20:53 CET 2006
I was writing a small script the other day with the following CLI
prog [options] [file]*
I've used getopt to parse out the possible options, so we'll ignore
that part, and assume for the rest of the discussion that args is a
list of file names (if any provided).
I used this bit of code to detect wether i want stdinput or not.
args = [ sys.stdin ]
Now in my main loop I've written:
for file in args:
for line in open( file ):
The probelm occurs when I pass no arguments and python trys to open(
open() customarily accepts a pathname, and returns a file object.
But this code is so elegant I thought that maybe, if open were passed
file object it could re-open that file in a new mode (if a mode was
provided different from the current mode) or simply return the file
object it was passed.
Anyhow, what do you guys think? There might be something about my idea
that makes this proposed behavior of open() unexpected, and I just
haven't thought of it.
More information about the Python-list