sys 模块笔记
sys.argv
实现在程序外部传递参数
returns a list of command line arguments passed to a Python script.
返回值为一个命令行列表
list中第0位一般为这个脚本的名字
The item at index 0 in this list is always the name of the script. The rest of the arguments are stored at the subsequent indices.
import sys
print("Hello {}. Welcome to {} tutorial".format(sys.argv[1],sys.argv[2]))
#.format()功能与 %s类似
输出结果:
python test1.py Star python
Hello Star. Welcome to python tutorial.
在Hardway 中理解
if len(sys.argv) == 2 and sys.argv[1] == "english":
#如果sys.argv返回的列表长度为2 且 argv[2]为“english”