catch empty sys.argv

Tomasz Lisowski lisowski.tomasz at sssa.NOSPAM.com.pl
Thu Jul 12 03:32:45 EDT 2001


U¿ytkownik "pedro" <pedro at athlet.de> napisa³ w wiadomo¶ci
news:3b4d491b.56611422 at news.isis.de...
> hi,
>
> my scripts gets a variable from .sys.argv[1] (a filename). when the
> filename is not entered the scripts is completely useless, so i
> thought i'd do a basic check like:
>
> if sys.argv[1] == ''
>     exit('enter something, stupid')

Why don't you first check the length of the sys.argv list?

Example:
nArgs = len(sys.argv)
if n == 0:
    exit('enter something, stupid')

or alternatively, define a function:

def arg(n):
    try:
        res = sys.argv[n]
    except IndexError:
        res = ""
    return res

and use it:

if arg(1) == ''
    exit('enter something, stupid')

Tomasz Lisowski





More information about the Python-list mailing list