stdin or optional fileinput

Steve Holden steve at holdenweb.com
Wed Mar 15 19:14:30 CET 2006


the.theorist wrote:
> Steven Bethard wrote:
> 
>>the.theorist wrote:
>>
>>>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.
>>>
>>>if len(args)==0:
>>>    args = [ sys.stdin ]
>>>
>>>Now in my main loop I've written:
>>>
>>>for file in args:
>>>    for line in open( file ):
>>>        #do stuff
>>
>>You should probably write:
>>
>>if not args:  # note that len(args) == 0 is repetitively redundant, over
>>               # and over again, in a reiterative manner
>>     files = [sys.stdin]
>>else:
>>     files = (open(filename) for filename in args)
>>
>>...
>>
>>for fileobj in files:  # using the name 'file' is a bad idea since it
>>                        # shadows the builtin 'file'
>>     for line in fileobj:
>>         # do stuff
>>
>>
>>STeVe
> 
> 
> I'll keep both those in mind for future programs.
> my current fix has been
> 
> if not args:
>     args = [ sys.stdin ]
> else:
>     map( open, args )
> 
> and then a modification to the main loop, as you proposed.
> 
> I thought that one day I might run into a problem opening too many
> files though (esp if i used xargs in a pipe). And it just _feels_
> better to open and close as I loop over the files list, rather than
> open everything at the get go.
> 
Yes, that feels most Pythonic.

> OH, i've noticed you used a generator that takes care of that. Except,
> the machine I'm working on is currently stuck at Python 2.3. Still,
> I'll keep your suggestions in mind for future coding.
> 
Well the nice thing about the generator expression is precisely its 
deferral of applying open() to the filename argument until the iterable 
element is extracted. But as you observe there's no use trying that in 
Python 2.3, so you'll have to adopt a slightly less elegant solution for 
now.

The important thing is that you seem to be well in touch with the 
various issues, so there's a good chance you won't go wrong.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd                 www.holdenweb.com
Love me, love my blog         holdenweb.blogspot.com




More information about the Python-list mailing list