3

I'm trying to explain to my friend, how sys.argv works. And I ran this snippet from command line (tested on cmd and bash).

python -c "import sys; print(sys.argv)" bla stas

But to my surprise this is the output:

['-c', 'bla', 'stas']

And I'm stumped - why the expression in quotes "import sys; print(sys.argv)" is not part of the argument list? Shouldn't it be at the second place like this:

['-c', '"import sys; print(sys.argv)"', 'bla', 'stas']

?

2 Answers 2

3

From the documentation:

argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'.

Putting the command itself in argv[] as well would be wrong, since it's not an argument to the script. The arguments are just bla and stas.

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

Comments

0

Python treats the -c argument as a special case and doesn't put the code string into sys.argv.

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.