5

I have a program in python 2.7 which accepts command line parameters using argparse, however if I try to enter a string containing an ampersand I lose the characters after that ampersand.

For example:

I have a simple python program just to test the input of command line arguments and simply prints out what was entered for a single command line parameter.

Essentially: print args.where

When I run the program with an argument like this:

$ python args.py -a http://www.website.com?optionone=one&numbertwo=two

The only text printed to the screen is: http://www.website.com?optionone=one

I have similar results when using sys and printIng using Argv

How can I get the full string entered?

2 Answers 2

5

This is a problem with your shell. Put the argument in quotes.

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

3 Comments

I'd noticed that quotes did work correctly but forgot to mention that in the question. There is no way around using them?
@John did you use double quotes or single? IIRC, single quotes should protect the ampersand...
@John: it depends on the shell you're using. If the shell considers an ampersand to be a character with special meaning to the shell (as I think nearly all of them do - even cmd.exe on Windows), then you'll need to do something to get the shell to pass that character as part of the argument. Quoting is one common way to do that, escaping the character (as Daniel Scott mentions) is another.
0

You can escape the ampersand with a backslash:

$ python args.py -a http://www.website.com?optionone=one\&numbertwo=two

Comments

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.