python:sys.argv [0]在官方文档中的含义(python: sys.argv[0] meaning in official documentation)

系统教程 行业动态 更新时间:2024-06-14 16:58:30
python:sys.argv [0]在官方文档中的含义(python: sys.argv[0] meaning in official documentation)

从docs.python.org引用:

“ sys.argv传递给Python脚本的命令行参数列表argv[0]是脚本名称(不管它是否是完整路径名都取决于操作系统)如果命令是使用-c命令执行的将选项argv[0]设置为字符串'-c' ,如果没有脚本名称传递给Python解释器,则argv[0]是空字符串。

我是否缺少一些东西,或者sys.argv[0]总是返回脚本名称,并得到'-c'我不得不使用sys.argv[1] ?

我正在使用GNU / Linux上的Python 3.2进行测试。

Quoting from docs.python.org:

"sys.argv The list of command line arguments passed to a Python script. 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'. If no script name was passed to the Python interpreter, argv[0] is the empty string."

Am I missing something, or sys.argv[0] always returns the script name, and to get '-c' I'd have to use sys.argv[1]?

I'm testing with Python 3.2 on GNU/Linux.

最满意答案

不,如果您使用-c从命令行运行命令来调用Python,则您的sys.argv[0]将为-c :

C:\Python27>python.exe -c "import sys; print sys.argv[0]" -c

No, if you invoke Python with -c to run commands from the command line, your sys.argv[0] will be -c:

C:\Python27>python.exe -c "import sys; print sys.argv[0]" -c

更多推荐

本文发布于:2023-04-15 03:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a9a52777f9eba4b9065c289ce413d4d1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:含义   文档   官方   argv   sys

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!