简体   繁体   中英

What is Python first argument? sys.argv[0] or sys.argv[1]

According to https://www.tutorialspoint.com/python/python_command_line_arguments.htm

first argument is always script name and it is also being counted in number of arguments.

which is sys.argv[0]

However, when I read other tutorial such as

https://www.cyberciti.biz/faq/python-command-line-arguments-argv-example/

it says that first argument is sys.argv[1]

#!/usr/bin/python
__author__ = 'nixCraft'
import sys total = len(sys.argv)
cmdargs = str(sys.argv)
print ("The total numbers of args passed to the script: %d " % total)
print ("Args list: %s " % cmdargs)
# Pharsing args one by one
print ("Script name: %s" % str(sys.argv[0]))
print ("First argument: %s" % str(sys.argv[1]))
print ("Second argument: %s" % str(sys.argv[2]))

Which one is correct and should be followed?

This is confusing especially to those who just started learning programming and Python.

sys.argv[0] is the name of the script. Technically it is the "first" argument, but is typically of no use to you, unless you don't know the name of the file you're executing. sys.argv[1] , is the name of the first argument after the name of the script, so the first useful argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM