command line arguments?

george young gry at ll.mit.edu
Wed Aug 14 22:50:38 CEST 2002


On Wed, 14 Aug 2002 13:55:54 -0400
"Steve Holden" <sholden at holdenweb.com> wrote:

> <brobbins333 at shaw.ca> wrote in message news:3d5a977e.9834056 at news...
> > Is it possible to invoke a Python script with command line arguments
> > in the manner of C?
> >
> >
> > Something like this (not in the interactive interpreter):
> >
> > C:\> python myscript.py arg1 arg2
> 
> Yes, using sys.argv. Supposing "myscript.py" reads:

Also, don't forget about getopts,e.g.:

import getopts

try:
    opts, args = getopt.getopt(sys.argv[1:], 'hz')
except getopt.error, errmsg:
    print >>sys.stderr, errmsg
    print >>sys.stderr, usage
    sys.exit(2)

ident ='This is %s version %s last edited by %s on %s' % (programname, cvs_rev, cvs_author, cvs_date)

for o,a in opts:
    if o == '-h':
        print __doc__
        print ident
        print usage
        sys.exit(0)
    elif o == '-z':
        debug = 1
    else:
        print usage
        sys.exit(1)


-- 
 I cannot think why the whole bed of the ocean is
 not one solid mass of oysters, so prolific they seem. Ah,
 I am wandering! Strange how the brain controls the brain!
	-- Sherlock Holmes in "The Dying Detective"



More information about the Python-list mailing list