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

查看:38
本文介绍了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天全站免登陆