#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0

    Addtional argument parameter problems.[little problem]


    Hi, I'm new to Python,

    I don't know how to make my program detect some additional arg. parameters. For example, normally we type:

    python filename.py

    to run the program. But if we have additional parameter such as:

    python filename.py -a

    and I want my program to be able to detect that "-a" so it will perform some additional function. How do I do that? I try to find in google, but I couldn't find anything on that. So please help me >_<

    Thx.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    You can choose:
    1) sys.argv
    Code:
    # test.py
    import sys
    print sys.argv
    Let's say you call the program with test.py -a -b, the output will be
    Code:
    ['test.py', '-a', '-b']
    2) there are some useful modules: getopt, optparse, getargs

    Hope this helps.

    Regards, mawe
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Arguments passed to Python programs via the command line can be accessed though the sys.argv list. In this list the first argument will be the name of the Python program being run, after this come the rest of the arguments… if any where passed to it that is .

    An interesting point to note: from the interactive prompt, sys.argv is empty (not strictly true, it contains an empty string for programs name). However you can still manually add/remove things from the list. This is handy should you want to test some code that relies on arguments in sys.argv or maybe turn a command line program into something clickable under Windows or Mac OS X.

    Code:
    >>> import sys
    >>> sys.argv
    ['']
    >>> sys.argv[0] = 'interactive.py'
    >>> sys.argv
    ['interactive.py']
    >>>
    Here's an example of how you could write a setup.py file for py2exe so that it can be run by double clicking it; from Grims py2exe walk through .

    Code:
    from distutils.core import setup
    import py2exe
    import sys
    sys.argv.append("py2exe")
    setup(console = [{"script": 'prog.py'}]
    Take care,

    Mark.
    Last edited by netytan; February 18th, 2005 at 03:58 PM.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0
    thx for your replies guys,

    ok, well it seem that I have to use getopt.

    However, I've another question, normally I push F5 to run my python program...but since there are some arguements, I need to type it instead...but I got an syntax errors

    python C:\Documents and Settings\All\Desktop\getopt.py -a

    I got a syntax error for that...I dont' know why
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The syntax error has to do with your program, if you want more help with this you'll need to post the code causing the problem.

    Another problem you might have:

    The first path on Pythons search path (sys.path) is your programs local directory. This means that by importing the getopt module you're program is accidentally trying to import Python itself . Luckily the fix is very simply, just rename your program so that it doesn't conflict with any of the modules being imported and you should be fine.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    24
    Rep Power
    0
    *EDIT*, I post a new thread for it.

IMN logo majestic logo threadwatch logo seochat tools logo