newbie question...

Discussion in 'Python' started by trfilmographer@gmail.com, Dec 12, 2008.

  1. Guest

    Hi!

    Im new at python and I just want to know if (and how) it is possible
    to send parameters to a program.

    what I mean is that when we start python I can call a file that should
    be run like this: python myfile.py
    can I send additional parameters along with it? like::: python
    myfile.py myVar1 myVar2

    Thanks!!!
     
    , Dec 12, 2008
    #1
    1. Advertising

  2. Tim Chase Guest

    > Im new at python and I just want to know if (and how) it is possible
    > to send parameters to a program.
    >
    > what I mean is that when we start python I can call a file that should
    > be run like this: python myfile.py
    > can I send additional parameters along with it? like::: python
    > myfile.py myVar1 myVar2


    You're looking for sys.argv:

    tim@rubbish:~$ python - one two
    Python 2.5.2 (r252:60911, May 28 2008, 08:35:32)
    [GCC 4.2.4 (Debian 4.2.4-1)] on linux2
    Type "help", "copyright", "credits" or "license" for more
    information.
    >>> import sys
    >>> sys.argv

    ['-', 'one', 'two']


    -tkc
     
    Tim Chase, Dec 12, 2008
    #2
    1. Advertising

  3. r Guest

    yes, but your script will need to know hoe to handle this.the
    following will open a file who's name was passed to the script

    if len(sys.argv) > 1:
    try:
    open_file(fname=sys.argv[1])
    except:
    pass
     
    r, Dec 12, 2008
    #3
  4. Guest

    On Dec 12, 12:59 pm, r <> wrote:
    > yes, but your script will need to know hoe to handle this.the
    > following will open a file who's name was passed to the script
    >
    > if len(sys.argv) > 1:
    >     try:
    >         open_file(fname=sys.argv[1])
    >     except:
    >         pass




    ah, ok. now what if I want the variable to be an integer that I
    send? for instance if I send 99 to the program, it is picking it up
    as a string instead of an integer value. How do I handle this with
    python??
     
    , Dec 12, 2008
    #4
  5. r Guest

    i was just giving you an example from my TextEditor.py script.

    this is how arg works
    lets say you have a script named MyScript.py
    ---
    import sys
    print sys.argv
    ---
    call the script with arguments
    > MyScript.py 99 100

    ['C:\\Python25\\MyScript.py', '99', '100']


    int(sys.argv[1]) -> 99
     
    r, Dec 12, 2008
    #5
  6. Tim Rowe Guest

    2008/12/12 <>:

    > ah, ok. now what if I want the variable to be an integer that I
    > send? for instance if I send 99 to the program, it is picking it up
    > as a string instead of an integer value. How do I handle this with
    > python??


    As 'r' has said, you can cast it to integer. In the real world you'd
    want to handle the case where the user passes in something that isn't
    an integer, or passes in nothing at all -- it's generally a good idea
    to check anything the user enters for validity. It's possibly too
    early for you to try to code it, but it's never too early to start
    thinking about what you would /want/ it to do. What do you think it
    will do for invalid or no command line arguments at the moment? Try
    it!

    --
    Tim Rowe
     
    Tim Rowe, Dec 12, 2008
    #6
    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. Jerry C.
    Replies:
    8
    Views:
    279
    Uri Guttman
    Nov 23, 2003
  2. Kruno Saho
    Replies:
    0
    Views:
    169
    Kruno Saho
    Apr 7, 2013
  3. Dave Angel
    Replies:
    0
    Views:
    147
    Dave Angel
    Apr 7, 2013
  4. rusi
    Replies:
    0
    Views:
    133
  5. Miki Tebeka
    Replies:
    0
    Views:
    95
    Miki Tebeka
    Apr 7, 2013
Loading...

Share This Page