Access to sys.argv when python interpreter is invoked in some modes like 'python -c "command"'

Simon Forman rogue_pedro at yahoo.com
Sun Aug 20 14:34:48 EDT 2006


poggle.themammal at gmail.com wrote:
> The python tutorial says
> "When the script name is given as '-' (meaning standard input),
> sys.argv[0] is set to '-'. When -c command is used, sys.argv[0] is
> set to '-c'. " but when we use a command say 'python -c "command"'
> where can we access "sys.argv"  (are there some commands where
> "sys.argv" is accessed. I can run 'python -c "import sys sys.argv", I
> get an error message
>
> -Tiro

When you get an error message, and you ask a question concerning the
error message, *post* the error message.

But first, read it yourself.  It will probably tell you what's wrong.
That's why it exists.

Meantime, what's your question?


BTW, "import sys sys.argv"  is invalid python.  When putting code
statements together on one line like this you must separate them with
";".  I.e. "import sys; sys.argv".   Note that this will still not do
what (I'm guessing) you want because this code is not being executed
interactively.  It will simply "get" the value of sys.argv and then
throw it away.  Try "import sys; print sys.argv",  if you want to print
the value of sys.argv.

Happy coding,
~Simon




More information about the Python-list mailing list