Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (0)

Powered by Vanilla. Made with Bootstrap.
Python sys.argv Problem
  • Xin
    Posts: 3,251
    I know theres problem a simple answer but i cant figure it out.

    Basic im passing a string using the following code


    import sys

    print sys.argv[1]


    But the sys.argv[1] that is printed has a "-" before it, so it doesnt work with what im doing with it, which is using it in the urllib
    Xin
  • chroniccommand
    Posts: 1,389
    said:


    I know theres problem a simple answer but i cant figure it out.

    Basic im passing a string using the following code


    import sys

    print sys.argv[1]


    But the sys.argv[1] that is printed has a "-" before it, so it doesnt work with what im doing with it, which is using it in the urllib


    A better approach would be using optparser or argparse. Personally I prefer optparser. It allows you to make scripts that have arguments like

    python script.py -i 127.0.0.1

    Etc. Check it out here
    http://docs.python.org/library/optparse.html
  • sangf
    Posts: 203
    there shouldn't be '-' before afaik, for example with: script.py hello as your example code, it should print 'hello'. this is the case for me. you could always just parse it away but find out why it's there first :P