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
View Code

相关文章:

  • 2021-12-06
  • 2022-12-23
  • 2021-07-17
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
猜你喜欢
  • 2021-04-04
  • 2021-06-26
  • 2021-12-26
  • 2021-10-14
  • 2021-12-01
  • 2021-09-06
相关资源
相似解决方案