python中sys的用法
常见用法: sys.argv
sys.argv用来获取当前正在执行的命令行参数的列表.
sys.argv[0]是程序名, sys.argv[1]是第一个参数, 以此类推后边的参数.
其他用法暂不介绍.
参考代码:
# encoding: utf-8
# filename: argv_test.py
import sys
# 获取脚本名字
print('The name of this program is: %s' %(sys.argv[0]))
# 获取参数列表
print('The command line arguments are:')
for i in sys.argv:
print(i)
# 统计参数个数
print('There are %s arguments.'%(len(sys.argv)-1))
运行结果:
E:\p>python argv_test.py arg1 arg2 arg3
The name of this program is: argv_test.py
The command line arguments are:
argv_test.py
arg1
arg2
arg3
There are 3 arguments.