sys模块的功能
sys是python中较为常用的一个模块,他提供了对python脚本运行时的环境的操作。
sys功能:
1 sys.argv #将python脚本运行时的脚本名以及参数作为一个list,并输出。
# test_py.py文件 #/usr/bin/python3 import sys print('the script name is:',sys.argv[0]) if len(sys.argv) > 1: print("there are", len(sys.argv)-1, "arguments:") # 使用len(sys.argv)-1采集参数个数-1为减去[0]脚本名称 for arg in sys.argv[1:]: #输出除了[0]外所有参数 print(arg) else: print("there are no arguments!") [root@slyoyo ~]# python3 test_py.py haha the script name is: test_py.py there are 1 arguments: haha