2

How can I input the parameter to my py via cmd and include '&' symbol?

for example, I'd like to input the parameter in cmd like this:

python test.py Test test2&test3 "test4"

I suppose to get 'Test' by sys.argv[1], 'test2&test3' by sys.argv[2], '"test4"' by sys.argv[3]

However when I try it, it throw error

brand = user_input[1] 
IndexError: list index out of range 
'test3' is not recognized as an internal or external command

I think the reason will be '&' symbol was trigger as 'end' something (not sure & meaning in python cmd) so it end the input of sys.argv

I tried input it like test2&test3, test2\&test3 . But still not work.

It's possible for me to input & as my program parameter from cmd?

Fong Tom
  • 87
  • 5

1 Answers1

1

This is not a Python issue, it is the command processor doing this. You would have the same problem in any programming language. When you use a special character such as & or even a space, you need to put that argument in quotes. Since you mention cmd I assume you are on Windows, but this would also be the case in a Linux or macOS shell.

If you do it like this you will get the result you expect:

python test.py Test "test2&test3" "test4"
Michael Geary
  • 28,450
  • 9
  • 65
  • 75