passing arguments

Steven Bethard steven.bethard at gmail.com
Sun May 22 18:43:45 EDT 2005


Steve Holden wrote:
> Steven Bethard wrote:
>>
>> Also possible, to guarantee that exactly one argument was given:
>>
>> try:
>>    arg1, = sys.argv
>> except ValueError:
>>    print "This script takes an argument, you boob!"
>>    sys.exit(1)
>>
> Aren't we forgetting argv[0] here

Oops.  Yup.  Change all "sys.argv" to "sys.argv[1:]".  Sorry, I never 
use sys.argv directly anymore; I always get my argv through optparse, 
which kindly strips off sys.argv[0].  Good catch, thanks!

STeVe



More information about the Python-list mailing list