command line argument passing

Discussion in 'Python' started by Hayri ERDENER, Jul 22, 2005.

  1. hi to all,
    is it possible in python to pass arguments by using command prompt in
    windows 2000 and XP ?
    for example:
    sourceCode.py factorial 4
    gives me the factorial of 4 namely 24.
    best regards
     
    Hayri ERDENER, Jul 22, 2005
    #1
    1. Advertising

  2. Hayri ERDENER

    Peter Hansen Guest

    Hayri ERDENER wrote:
    > hi to all,
    > is it possible in python to pass arguments by using command prompt in
    > windows 2000 and XP ?
    > for example:
    > sourceCode.py factorial 4
    > gives me the factorial of 4 namely 24.


    Yes, the arguments are available as a list in sys.argv .

    Remember that you need to import "sys" before you can access that, and
    that the values will be returned as strings, so if they should be
    treated as numbers it's up to you to convert them.

    Use of the getopt or optparse modules is recommended to work with
    command line arguments more effectively.

    -Peter
     
    Peter Hansen, Jul 22, 2005
    #2
    1. Advertising

  3. Hayri ERDENER

    gene tani Guest

    gene tani, Jul 22, 2005
    #3
  4. Hayri ERDENER schrieb:
    > hi to all,
    > is it possible in python to pass arguments by using command prompt in
    > windows 2000 and XP ?
    > for example:
    > sourceCode.py factorial 4
    > gives me the factorial of 4 namely 24.
    > best regards


    import sys

    print sys.argv


    Or use the optparse module. Docs:
    http://www.python.org/doc/2.4.1/lib/module-optparse.html

    Bye,
    Dennis
     
    Dennis Benzinger, Jul 22, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. virgincita schmidtmann

    passing command line argument to array

    virgincita schmidtmann, May 4, 2007, in forum: C Programming
    Replies:
    6
    Views:
    458
    Old Wolf
    May 6, 2007
  2. Alan Biddle
    Replies:
    14
    Views:
    2,580
    John Bokma
    Feb 5, 2010
  3. Trans
    Replies:
    3
    Views:
    198
    Robert Klemme
    Jan 26, 2005
  4. kenny roytman
    Replies:
    0
    Views:
    137
    kenny roytman
    Oct 30, 2007
  5. ±è ÁØ¿µ
    Replies:
    7
    Views:
    254
    Einar Magnús Boson
    Nov 27, 2008
Loading...

Share This Page