|
List Info
Thread: how to send command line args to py script
|
|
| how to send command line args to py
script |

|
2007-03-11 20:23:26 |
lo there all,
i was wondering how to make a python script accept command
line arguments.
i mean, i have used python scripts from the command line in
linux and
passed something to it and it knows what to do.
like in a function, if i want to do something like this
def add_two_numbers(a, b):
x = a + b
return x
x = add_two_numbers(4,5)
print x
how could i do the same from the cli.
like python add_two_numbers.py 4 5
or maybe python add_two_numbers.py 4, 5
or even python add_two_numbers.py -a 4 -b 5
is there an easy way to do this ?
thanks
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Re: how to send command line args to py
script |
  United States |
2007-03-11 20:55:41 |
This is my first time replying to the list, so excuse me if
this goes
out wrong.
Anyhow, you're looking for sys.agrv. sys.agrv is a list of
the
arguments, with sys.agrv[0] being the script name.
Code:
##########
import sys #sys.argv is part of the sys module
def add_two_numbers(a, b):
x = a + b
return x
y = float(sys.argv[1]) #This gets the arguments and converts
them to numbers
z = float(sys.argv[2]) #You need to do this because the
arguments come
as strings
x = add_two_numbers(y,z) #Of course, that means if you put
in "python
add.py 1 fish"
print x #you'll get an error. So a try-except clause would
be good here.
##########
Run the above script with the formatting "python
(filename).py 1 1", not
"python (filename).py -1 -1", that'll get you
negative numbers.
shawn bright wrote:
> lo there all,
>
> i was wondering how to make a python script accept
command line arguments.
> i mean, i have used python scripts from the command
line in linux and
> passed something to it and it knows what to do.
>
> like in a function, if i want to do something like
this
>
> def add_two_numbers(a, b):
> x = a + b
> return x
>
> x = add_two_numbers(4,5)
> print x
>
> how could i do the same from the cli.
>
> like python add_two_numbers.py 4 5
> or maybe python add_two_numbers.py 4, 5
> or even python add_two_numbers.py -a 4 -b 5
>
> is there an easy way to do this ?
>
> thanks
> _______________________________________________
> Tutor maillist - Tutor python.org
> http://
mail.python.org/mailman/listinfo/tutor
>
>
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Re: how to send command line args to py
script |

|
2007-03-11 21:07:41 |
Well, for your first response, its great,
thanks a lot.
shawn
On 3/11/07, Gordon <chaosweevil42 gmail.com> wrote:
> This is my first time replying to the list, so excuse
me if this goes
> out wrong.
>
> Anyhow, you're looking for sys.agrv. sys.agrv is a
list of the
> arguments, with sys.agrv[0] being the script name.
>
> Code:
> ##########
> import sys #sys.argv is part of the sys module
> def add_two_numbers(a, b):
> x = a + b
> return x
> y = float(sys.argv[1]) #This gets the arguments and
converts them to numbers
> z = float(sys.argv[2]) #You need to do this because the
arguments come
> as strings
> x = add_two_numbers(y,z) #Of course, that means if you
put in "python
> add.py 1 fish"
> print x #you'll get an error. So a try-except clause
would be good here.
> ##########
> Run the above script with the formatting "python
(filename).py 1 1", not
> "python (filename).py -1 -1", that'll get you
negative numbers.
>
> shawn bright wrote:
> > lo there all,
> >
> > i was wondering how to make a python script accept
command line arguments.
> > i mean, i have used python scripts from the
command line in linux and
> > passed something to it and it knows what to do.
> >
> > like in a function, if i want to do something like
this
> >
> > def add_two_numbers(a, b):
> > x = a + b
> > return x
> >
> > x = add_two_numbers(4,5)
> > print x
> >
> > how could i do the same from the cli.
> >
> > like python add_two_numbers.py 4 5
> > or maybe python add_two_numbers.py 4, 5
> > or even python add_two_numbers.py -a 4 -b 5
> >
> > is there an easy way to do this ?
> >
> > thanks
> > _______________________________________________
> > Tutor maillist - Tutor python.org
> > http://
mail.python.org/mailman/listinfo/tutor
> >
> >
>
> _______________________________________________
> Tutor maillist - Tutor python.org
> http://
mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Re: how to send command line args to py
script |
  United States |
2007-03-11 22:21:12 |
On Sun, Mar 11, 2007 at 09:07:41PM -0500, shawn bright
wrote:
> Well, for your first response, its great,
> thanks a lot.
> shawn
And, when you needs to process command line arguments and
flags and
options become more complicated, be sure to look at modules
getopt
and optparse in the standard library. optparse is newer and
more
powerful than getopt, but also seems a bit more complex.
Dave
--
Dave Kuhlman
http://www.rexx.com/~dk
uhlman
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
| Re: how to send command line args to py
script |

|
2007-03-12 00:17:37 |
OK, will do. Looks like my future will involve more of this
kind of thing.
thanks
shawn
On 3/11/07, Dave Kuhlman <dkuhlman rexx.com> wrote:
> On Sun, Mar 11, 2007 at 09:07:41PM -0500, shawn bright
wrote:
> > Well, for your first response, its great,
> > thanks a lot.
> > shawn
>
> And, when you needs to process command line arguments
and flags and
> options become more complicated, be sure to look at
modules getopt
> and optparse in the standard library. optparse is
newer and more
> powerful than getopt, but also seems a bit more
complex.
>
> Dave
>
>
> --
> Dave Kuhlman
> http://www.rexx.com/~dk
uhlman
> _______________________________________________
> Tutor maillist - Tutor python.org
> http://
mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist - Tutor python.org
http://
mail.python.org/mailman/listinfo/tutor
|
|
[1-5]
|
|