从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]" -cNo, 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更多推荐
发布评论