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

查看:464
本文介绍了python:sys.argv [0]在官方文档中的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

docs.python.org 引用:

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

"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."

我是否缺少某些内容,或者sys.argv[0]总是返回脚本名称,而要获取'-c',我必须使用sys.argv[1]吗?

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]?

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

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

推荐答案

否,如果您使用-c调用Python以从命令行运行命令,则您的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

这篇关于python:sys.argv [0]在官方文档中的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆