[Tutor] Testing for commandline args

Max Noel maxnoel_fr at yahoo.fr
Sat May 14 02:39:43 CEST 2005


On May 14, 2005, at 01:30, William O'Higgins wrote:

> if sys.argv[1]:
>     do stuff
> else:
>     do different stuff
>
> If I have arguments, the "different stuff" happens beautifully, thank
> you very much.  If I don't have arguments I get this:
>
> if sys.argv[1]:
> IndexError: list index out of range]
>
> So I'm doing something wrong.  I looked at getopt, but that seemed  
> to be
> doing what I was already doing, except in a way I could not follow :-(

     How about this?


if len(sys.argv) > 1:
     # do stuff
else:
     # do different stuff


     Also, I hear that optparse is much better than getopt.

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting  
and sweating as you run through my corridors... How can you challenge  
a perfect, immortal machine?"



More information about the Tutor mailing list