1

I'm trying to read command line arguments in python in the form:

python myprogram.py string string string

I have tried using sys.argv[1-3] to get each string, but when I have a string such as $unny-Day, it does not process the entire string. How can I process strings like these entirely?

2
  • 1
    Could you add your specific code example that is failing? This will help troubleshoot your exact issue. Commented Feb 8, 2016 at 18:01
  • 1
    Are you putting $unny-Day in quotes when you pass it as an argument? Commented Feb 8, 2016 at 18:01

1 Answer 1

5

Are you using a shell? $ is a special character in the shell that is interpreted as a shell variable. Since the variable does not exist, it is textually substituted with an empty string.

Try using single quotes around your parameter, like > python myapp.py '$unny-Day'.

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

8 Comments

Exactly this. It is always safer to pass arguments that are strings as a string since some characters can be reserved.
Thanks, this solved the problem, but I need a way to process it as a string without using single quotes. Or, just after python myprogram.py, process three strings delimited by spaces.
I agree with @kazagistar but I tested it with several different string and quotes and sometimes they disapear, or they appear partially or they are replaced by an empty string. Try "$a" or "$sunny-day" to see the effects.
@user3808889 How are you calling your python script that adding single quotes is a problem? Are you calling it from some other program? If you specify which, we might be able to help better.
@user3808889 again, it depends on the application making the call. In the application "bash", for example, the way to escape special characters is to single quote the strings, or escape the characters with a \\ directly. But if you are calling out of another application, there is often a better way.
|

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.