官术网_书友最值得收藏!

sys.argv

To handle command-line arguments, Python uses sys.argv. In this sense, when a program is executed, Python takes all the values from the command line and sets them in the sys.argv list. The first element of the list is the full path to the script (or the script name—it is operating system dependent), which is always sys.argv[0]. The second element of the list is the first argument to the script, which is sys.argv[1], and so on. This can be seen in the following diagram, where the sysargv_python.py script is executed with two arguments:

To see how sys.argv works, we are going to use the sysargv_python.py script:

# Import the required packages
import sys

# We will print some information in connection with sys.argv to see how it works:
print("The name of the script being processed is: '{}'".format(sys.argv[0]))
print("The number of arguments of the script is: '{}'".format(len(sys.argv)))
print("The arguments of the script are: '{}'".format(str(sys.argv)))

If we execute this script without any parameter, we will see the following information:

The name of the script being processed is: 'sysargv_python.py'
The number of arguments of the script is: '1'
The arguments of the script are: '['sysargv_python.py']'

Additionally, if we execute this script with one parameter (for example, sysargv_python.py OpenCV), we will get the following information:

The name of the script being processed is: 'sysargv_python.py'
The number of arguments of the script is: '2'
The arguments of the script are: '['sysargv_python.py', 'OpenCV']'

As you can see, the first element, sysargv_python.py (sys.argv[0]), of the list is the script name. The second element, OpenCV, of the list (sys.argv[1]) is the first argument to our script.

argv[0] is the script name, which is operating system dependent if it is a full pathname or not. See https://docs.python.org/3/library/sys.html for more information.. 
主站蜘蛛池模板: 兰考县| 鄂伦春自治旗| 清河县| 岑巩县| 合水县| 乌恰县| 南江县| 奉贤区| 托里县| 台前县| 凌海市| 廉江市| 阿荣旗| 荣昌县| 固安县| 甘谷县| 泰顺县| 金堂县| 东乡| 容城县| 海门市| 惠东县| 盐池县| 蛟河市| 辉南县| 土默特左旗| 通河县| 漳浦县| 武宣县| 开阳县| 奇台县| 青海省| 渭南市| 华池县| 临澧县| 九龙坡区| 贵州省| 汝阳县| 尚义县| 永平县| 神农架林区|