Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

passing arguments

0 views
Skip to first unread message

Jeff Elkins

unread,
May 20, 2005, 6:26:09 PM5/20/05
to pytho...@python.org
I'm sure this is obvious, but how the heck do pass an argument(s) to a python
script from the command line?

Thanks,

Jeff Elkins

James Stroud

unread,
May 20, 2005, 6:46:53 PM5/20/05
to pytho...@python.org
import sys

try:
arg1 = sys.argv[1]
except IndexError:
print "This script takes an argument, you boob!"
sys.exit(1)

OR, way better: See the optparse module.

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/

Jeff Elkins

unread,
May 20, 2005, 7:20:50 PM5/20/05
to pytho...@python.org
On Friday 20 May 2005 06:46 pm, James Stroud wrote:
> import sys
>
> try:
> arg1 = sys.argv[1]
> except IndexError:
> print "This script takes an argument, you boob!"
> sys.exit(1)
>
> OR, way better: See the optparse module.
>
> On Friday 20 May 2005 03:26 pm, Jeff Elkins wrote:
> > I'm sure this is obvious, but how the heck do pass an argument(s) to a
> > python script from the command line?
> >
> > Thanks,
> >
> > Jeff Elkins

Thanks!

Steven Bethard

unread,
May 20, 2005, 9:38:56 PM5/20/05
to
James Stroud wrote:
> import sys
>
> try:
> arg1 = sys.argv[1]
> except IndexError:
> print "This script takes an argument, you boob!"
> sys.exit(1)

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)

If you want to get, say, 3 arguments, just change that line to:

arg1, arg2, arg3 = sys.argv

> OR, way better: See the optparse module.

Definitely. Though depending on what kind of arguments your script
takes, you still may need to deal with the args that optparse returns.

STeVe

Philippe C. Martin

unread,
May 22, 2005, 8:29:40 AM5/22/05
to
Hi,

look at sys.argv

Regards,

Philippe

Steve Holden

unread,
May 22, 2005, 4:12:06 PM5/22/05
to pytho...@python.org
Steven Bethard wrote:
> James Stroud wrote:
>
>>import sys
>>
>>try:
>> arg1 = sys.argv[1]
>>except IndexError:
>> print "This script takes an argument, you boob!"
>> sys.exit(1)
>
>
> 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, or am I overlooking something (like,
you chopped it off without telling me?)?

Surely this would give an unpacking error if command-line arguments
*were* present.

> If you want to get, say, 3 arguments, just change that line to:
>
> arg1, arg2, arg3 = sys.argv
>

Similarly. Or are you just calling sys.argv[0] arg1 to confuse me? ;-)


>
>>OR, way better: See the optparse module.
>
>
> Definitely. Though depending on what kind of arguments your script
> takes, you still may need to deal with the args that optparse returns.
>

Anyway it's always good to know about the underlying mechanisms.

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/

Message has been deleted

Steve Holden

unread,
May 22, 2005, 6:02:26 PM5/22/05
to pytho...@python.org
Dennis Lee Bieber wrote:
> On Sun, 22 May 2005 16:12:06 -0400, Steve Holden <st...@holdenweb.com>
> declaimed the following in comp.lang.python:

>
>
>
>>Aren't we forgetting argv[0] here, or am I overlooking something (like,
>>you chopped it off without telling me?)?
>>
>
> argv[0] is often just the invocation name of the program/script
> itself, not really an argument to the function of the program.
>
And you think I don't know this? I was questioning the correctness of
the assertions of the post to which I was replying. Whether it's a real
argument or not in undoubtedly occupies an entry in sys.argv.

> script1.py
> -=-=-=-=-=-=-=-=-
> import sys
>
> for x in range(len(sys.argv)):
> print "Argument %s is %s" % (x, sys.argv[x])
> -=-=-=-=-=-=-=-=-
>
> (watch out for line wrapping)
>
> E:\UserData\Dennis Lee Bieber\My Documents>script1.py "These are the"
> Voyages
> Argument 0 is E:\UserData\Dennis Lee Bieber\My Documents\Script1.py
> Argument 1 is These are the
> Argument 2 is Voyages
>
> E:\UserData\Dennis Lee Bieber\My Documents>python script1.py "quoted
> arg" regular args
> Argument 0 is script1.py
> Argument 1 is quoted arg
> Argument 2 is regular
> Argument 3 is args
>
> E:\UserData\Dennis Lee Bieber\My Documents>
>
>
So?

Steven Bethard

unread,
May 22, 2005, 6:43:45 PM5/22/05
to
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

0 new messages