2

I'm using python 3.3.0 in windows 8.

This is the code which I'm trying to deal with:-

import sys

for arg in sys.argv:
    if arg == "-u":
        url = sys.argv[2]
        print (url)
        sys.exit(1)

So, When I run my program in cmd like:

python my.py -u http://www.aytelecom.ae/news-details.php?id=5&type=2

I'm getting only 'http://www.aytelecom.ae/news-details.php?id=5' as my sys.argv[2] or in url variable!

So, can anybody tell me that why it's skipping '&type=2'? Am I doing mistake anywhere or what's wrong with python 3?

5
  • you could use sys.argv.index("-u") Commented Oct 24, 2012 at 12:54
  • Hmn, I didn't get you. Would you please elaborate it? Commented Oct 24, 2012 at 13:05
  • seq.index(x) returns the index of the first occurrence of x in seq or raises ValueError if x is not in seq. You could use it instead of the for-loop. Commented Oct 24, 2012 at 13:12
  • Yup, I got your point but I think it would be useful for only one parameter. What if I have more options to check for? And when I tried to run this: import sys sys.argv.index("-u") url = sys.argv[2] print ("URL:", url) sys.exit(1) It gives something in the last line like: The system cannot find the file specified. Commented Oct 24, 2012 at 13:27
  • if you have more parameters you could use argparse module Commented Oct 24, 2012 at 13:28

1 Answer 1

6

& is a metacharacter in shell. Put the url in quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

oh my god, it was so easy and I have wasted whole day on Google to get a solution! Anyway, Thanks for your quick response. It's really a nice site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.