python之sys系统模块
python之sys系统模块
python之系统模块——sys
常用如下
sys.argv #命令行参数list.第一个元素是程序本身路径 sys.exit[n] #退出程序,正常退出时exit[0] sys.version #获得python解释程序的版本信息 sys.maxint #最大的int值 sys.path #返回模块的搜素路径,初始化时使用pythonpath环境变量的值 sys.platform #返回操作系统平台名称
示例如下
>>> import sys >>> sys.version '3.6.8 (default, Nov 16 2020, 16:55:22) \n[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]' >>> sys.path ['', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/local/lib64/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages', '/usr/local/lib/python3.6/site-packages/cloud_init-19.1.11-py3.6.egg', '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages'] >>> sys.platform 'linux' # cat sys_copy.py #!/bin/python3 # -*- coding: utf-8 -*- import sys src_file=sys.argv[1] dst_file=sys.argv[2] with open (r'%s' %src_file,mode='rb') as read_f,\ open (r'%s' %dst_file,mode='wb') as write_f: for line in read_f: write_f.write(line) # python3 4.py 源文件路径:1 目标文件路径:1.html # cat 3.py #!/bin/python3 # -*- coding: utf-8 -*- import sys src_file=sys.argv[1] dst_file=sys.argv[2] with open (r'%s' %src_file,mode='rb') as read_f,\ open (r'%s' %dst_file,mode='wb') as write_f: for line in read_f: write_f.write(line) # python3 3.py py.py 4.py #cat 4.py