Thread: Sys.argv

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

    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0

    Sys.argv


    Hi,
    I have now one basic doubt. While trying to read the command line options, I find a strange behaviour of python interpreter. (on windows; not sure if this is the same in unix like systems). Could somebody give some pointers/clue why this happens.

    Code:

    import sys

    if __name__ == '__main__':
    print sys.argv

    The above code snippet prints just the file name when run as :
    D:\> get_option.py abc
    ['D:\\get_option.py']

    but prints all the arguments when invoked explicitly by python.

    D:\>python get_option.py abc
    ['.\\get_option.py', 'abc']
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Works like this in unix:

    $ chmod +x p.py # make "it" executable
    $ cat p.py # show "it"
    #! /usr/bin/python
    import sys
    print(sys.argv)
    $
    $
    $ ./p.py abc # run "it"
    ['./p.py', 'abc']
    $
    $
    $ python ./p.py abc # run "it"
    ['./p.py', 'abc']
    $
    $
    $ python -ic 'import p' abc # run "it"
    ['-c', 'abc']
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Works like this in unix:
    Code:
    $ chmod +x p.py  # make "it" executable
    $ cat p.py            # show "it"
    #! /usr/bin/python
    import sys
    print(sys.argv)
    $
    $
    $ ./p.py abc       # run "it"
    ['./p.py', 'abc']
    $
    $
    $ python ./p.py abc  # run "it"
    ['./p.py', 'abc']
    $
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo