Python sys模块(进阶篇)

来源:互联网 发布:日剧的价值观知乎 编辑:程序博客网 时间:2024/06/03 23:43

原文链接:点击打开链接

本文针对原文做了少量修改和大量的补充。。。

这篇文章主要介绍了Python标准库之sys模块使用详解,讲解了使用sys模块获得脚本的参数、处理模块、
使用sys模块操作 模块搜索路径、使用sys模块查找内建模块、使用sys模块查找已导入的模块,重定向输出以及重定向错误等使用案例。。。
sys模块提供了许多函数和变量来处理 Python 运行时环境的不同部分.
处理命令行参数:在解释器启动后, argv 列表包含了传递给脚本的所有参数, 列表的第一个元素为脚本自身的名称.
【1】使用sys模块获得脚本的参数(给程序在外部传递参数):

test.py文件:

#!usr/bin/env python#coding:utf-8import sysprint 'sys.argv=',sys.argvprint 'The script name is',sys.argv[0] #使用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 argelse:    print 'There is no argument!'

运行结果:
song@ubuntu:~$ python test.py
sys.argv= ['test.py']
The script name is test.py
There is no argument!
song@ubuntu:~$ python test.py argument1 argument2
sys.argv= ['test.py', 'argument1', 'argument2']
The script name is test.py
There are 2 arguments!
argument1
argument2
song@ubuntu:~$

【2】如果是从标准输入读入脚本, 脚本的名称将被设置为空串。
song@ubuntu:~$ python < test.py
sys.argv= ['']
The script name is 
There is no argument!

【3】如果把脚本作为字符串传递给python (在命令行下选用 -c 选项), 脚本名会被设置为 "-c"。
song@ubuntu:~$ python  -c "import sys;print sys.argv;print sys.argv[0]" arg1 arg2
['-c', 'arg1', 'arg2']
-c
song@ubuntu:~$
如果大家不明白,可以参考下man python
SYNOPSIS
       python [ -d ] [ -E ] [ -h ] [ -i ] [ -m module-name ] [ -O ]
              [ -Q argument ] [ -S ] [ -t ] [ -u ]
              [ -v ] [ -V ] [ -W argument ] [ -x ]
              [ -c command | script | - ] [ arguments ]

处理模块:
我们在使用模块的某一个功能前,需要用import,__import__命令导入。
那我们在执行import module_name的时候,python内部发生了什么呢?
简单的说,就是搜索module_name。根据sys.path的路径来搜索module.name

>>> import sys>>> sys.path['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']>>>
我们以后写好的模块就可以放到上面的某一个目录下,便可以正确搜索到了。

当然也可以添加自己的模块路径。sys.path.append(“my module path”)。

>>> sys.path.append('my module path')>>> sys.path['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', 'my module path']>>> 

path列表是一个由目录名构成的列表, Python 从中查找扩展模块( Python 源模块, 编译模块,或者二进制扩展).
启动 Python 时,这个列表根据内建规则, PYTHONPATH 环境变量的内容, 以及注册表( Windows 系统)等进行初始化.
由于它只是一个普通的列表, 你可以在程序中对它进行操作。。。

【4】操作sys模块中的搜索路径:

path.py文件:

#!usr/bin/env python#coding:utf-8import sysprint 'sys.path=',sys.pathprint 'sys.path has',len(sys.path),'members.'sys.path.insert(0,'/tmp')  #将路径插入到path[0]中print 'sys.path=',sys.pathprint 'sys.path has',len(sys.path),'members.'sys.path=[]  #删除path中所有路径print 'sys.path=',sys.pathprint 'sys.path has',len(sys.path),'members.'#上述对sys.path的修改,仅仅在当前文件中有效,并不会修改python标准库中的sys模块。。。
运行结果:
song@ubuntu:~$ python path.py
sys.path= ['/home/song', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
sys.path has 12 members.
sys.path= ['/tmp', '/home/song', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
sys.path has 13 members.
sys.path= []
sys.path has 0 members.

【5】使用sys模块查找内建模块:
builtin_module_names 元组包含 Python 解释器中所有内建模块的名称

find_module.py文件:

def find_module(module):    print module,'=>',    if module in sys.builtin_module_names: #查找内建模块是否存在        print '<biultin>'    else:        mod=__import__(module) #非内建模块 输出模块路径        print mod.__file__import sysprint 'sys.builtin_module_names=',sys.builtin_module_namesfind_module('os')find_module('sys')find_module('string')find_module('strop')find_module('zlib')
运行结果:
song@ubuntu:~$ python find_module.py
sys.builtin_module_names= ('__builtin__', '__main__', '_ast', '_bisect', '_codecs', '_collections', '_functools', '_heapq', '_io', '_locale', '_md5', '_random', '_sha', '_sha256', '_sha512', '_socket', '_sre', '_struct', '_symtable', '_warnings', '_weakref', 'array', 'binascii', 'cPickle', 'cStringIO', 'cmath', 'errno', 'exceptions', 'fcntl', 'gc', 'grp', 'imp', 'itertools', 'marshal', 'math', 'operator', 'posix', 'pwd', 'select', 'signal', 'spwd', 'strop', 'sys', 'syslog', 'thread', 'time', 'unicodedata', 'xxsubtype', 'zipimport', 'zlib')
os => /usr/lib/python2.7/os.pyc
sys => <biultin>
string => /usr/lib/python2.7/string.pyc
strop => <biultin>
zlib => <biultin>
song@ubuntu:~$

【6】使用sys模块查找已导入的模块(sys.modules):
This is a dictionary that maps module names to modules which have already been loaded.
This can be manipulated to force reloading of modules and other tricks.
Python.org手册里已经说的很明白了。
modules 字典包含所有加载的模块。 import 语句在从磁盘导入内容之前会先检查这个字典。
Python 在处理你的脚本之前就已经导入了很多模块.

>>> import sys>>> type(sys.modules)<type 'dict'>>>> sys.modules.keys()['copy_reg', 'sre_compile', '_sre', 'encodings', 'site', '__builtin__', 'sysconfig', '__main__', 'encodings.encodings', 'abc', 'posixpath', '_weakrefset', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'types', '_codecs', 'encodings.__builtin__', '_warnings', 'genericpath', 'stat', 'zipimport', '_sysconfigdata', 'warnings', 'UserDict', 'encodings.utf_8', 'sys', 'codecs', 'readline', '_sysconfigdata_nd', 'os.path', 'sitecustomize', 'signal', 'traceback', 'linecache', 'posix', 'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']>>> len(sys.modules.keys())43>>>

【7】使用sys模块获得当前平台:
sys.platform  返回当前平台,例如: "win32" "linux2" 等

# windows下:>>> import sys>>> sys.platform'win32'# Linux下:>>> import sys>>> sys.platform'linux2'>>>
大家都知道,当今的程序比较流行的是跨平台。简单的说就是这段程序既可以在windows下运行,
换到linux下也可以不加修改的运行起来,此时,sys.platform 就派上用场了。
假设,我们想实现一个清除终端程序,linux下用clear, windows下用cls
clean.py文件:

#!usr/bin/env python#coding:utf-8import sys,ososType=sys.platformif osType=='linux' or osType=='linux2':    command='clear'else:    command='cls'os.system(command)
【8】处理标准输出/输入:
标准输出和标准错误 (通常缩写为 stdout 和 stderr) 是内建在每一个 UNIX 系统中的管道。
当你 print 某些东西时,结果前往 stdout 管道;
例如:

>>> for i in range(3):...     print 'hello python'... hello pythonhello pythonhello python>>> import sys>>> for i in range(3):...     sys.stdout.write('hello python\n')... hello pythonhello pythonhello python
【9】当你的程序崩溃并打印出调试信息 (例如 Python 中的 traceback (错误跟踪) ) 的时候,信息前往 stderr 管道。。

>>> l=[1,2]>>> print l[10]Traceback (most recent call last):  File "<stdin>", line 1, in <module>IndexError: list index out of range
stdout 是一个类文件对象;调用它的 write 函数可以打印出你给定的任何字符串。
实际上,这就是 print 函数真正做的事情;它在你打印的字符串后面加上一个硬回车,然后调用 sys.stdout.write 函数。
在最简单的例子中,stdout 和 stderr 把它们的输出发送到相同的地方。
和 stdout 一样,stderr 并不为你添加硬回车;如果需要,要自己加上。。。
stdout 和 stderr 都是类文件对象,但是它们都是只写的(它们都没有 read 方法,只有 write 方法)
由于它们是类文件对象,因此你可以将其它任何 (类) 文件对象赋值给它们来重定向其输出。
【10】重定向输出:

>>> import sys>>> print 'hello python'     #标准输出hello python>>> saveout=sys.stdout       #在重定向前保存stdout,之后你还可以将其设回正常>>> flog=open('out.log','w') #打开一个新文件用于写入。如果文件不存在,将会被创建。如果文件存在,将被覆盖。>>> sys.stdout=flog          #所有后续的输出都会被重定向到刚才打开的新文件上。>>> print  'This message will be logged instead of displayed'# 这样只会将输出结果“打印”到日志文件中;屏幕上不会看到输出。。。>>> flog.close()             #关闭日志文件>>> sys.stdout=saveout       #将 stdout 设回原来的方式。>>> print 'ok'               #标准输出(已经将stdout恢复到原来的方式)ok>>> f=open('out.log','r')>>> f.read() #注意最后的'\n'(说明print会在要打印的字符串后面加上一个硬回车)'This message will be logged instead of displayed\n'
【11】重定向错误信息:

>>> import sys>>> flog=open('error.log','w')# 打开要存储调试信息的日志文件。>>> sys.stderr=flog #将新打开的日志文件的文件对象 赋值给stderr,以重定向标准错误。>>> raise Exception, 'this error will be logged'# 引发一个异常,没有在屏幕上打印出任何东西,所有正常的跟踪信息已经写进error.log#还要注意我们既没有显式关闭日志文件,也没有将 stderr 设回最初的值。#这样挺好,因为一旦程序崩溃 (由于引发的异常),Python 将替我们清理并关闭文件 

【12】打印到 stderr:
向标准错误写入错误信息是很常见的,所以有一种较快的语法可以立刻导出信息

>>> print 'hello python'   #标准输出hello python>>> import sys>>> print >> sys.stderr,'hello python' #将单个print语句重定向到stderr,而且不影响后面的print语句hello python>>> 
【13】print 语句的快捷语法可以用于写入任何打开的文件 (或者是类文件对象)。

>>> f=open('print.txt','w')>>> print >>f,'hello python' #将单个print语句重定向到打开的文件对象f>>> f.close()>>> f=open('print.txt','r')>>> f.read()'hello python\n' # print会在要打印的字符串后面加上一个硬回车>>> f.close()
【14】使用sys模块退出程序:
执行至主程序的末尾时,解释器会自动退出. 但是如果需要中途退出程序,我们可以调用sys.exit 函数,
它带有一个可选的整数参数返回给调用它的程序. 这意味着你可以在主程序中捕获对sys.exit 的调用。
(注:0是正常退出,其他为不正常,可抛异常事件供捕获!)

>>> import sys>>> sys.exit(1)song@ubuntu:~$ #注意 sys.exit 并不是立即退出,而是引发一个 SystemExit 异常。#这意味着我们可以在主程序中捕获对 sys.exit 的调用
捕获sys.exit调用(1):
exit.py文件:

#!usr/bin/env python#coding:utf-8import sysprint 'hello python'try:    sys.exit(1)except SystemExit: #捕获到退出异常,,,程序不会退出,后边的语句会继续执行。。。    pass           #捕获后不做任何操作print 'ok'
运行结果:
song@ubuntu:~$ python exit.py
hello python
ok

捕获sys.exit调用(2):
exitfunc.py文件:
#!usr/bin/env python#coding:utf-8import sysdef exitfunc(value):    '''clear function'''    print value    sys.exit(0) #没有去捕获 抛出的SystemExit异常,执行到该语句时退出解释器print 'hello!'try:    sys.exit(1)  #抛出的SystemExit异常,被成功捕获except SystemExit,value:    exitfunc(value)print 'not out!' #该语句不会执行

输出结果:
song@ubuntu:~$ python exitfunc.py
hello!
1
song@ubuntu:~$
以下是python.org库参考手册中,摘抄来的,供参考。
Exit from Python. This is implemented by raising the SystemExit exception, 
so cleanup actions specified by finally clauses of try statements are honored, 
and it is possible to intercept the exit attempt at an outer level.
The optional argument arg can be an integer giving the exit status (defaulting to zero),
or another type of object. If it is an integer, zero is considered “successful termination” 
and any nonzero value is considered “abnormal termination” by shells and the like.
Most systems require it to be in the range 0-127, and produce undefined results otherwise. 
Some systems have a convention for assigning specific meanings to specific exit codes,
but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors 
and 1 for all other kind of errors. If another type of object is passed, 
None is equivalent to passing zero, and any other object is printed to sys.stderr 
and results in an exit code of 1. In particular,
sys.exit("some error message") is a quick way to exit a program when an error occurs.
大概意思是说,sys.exit从python程序中退出,将会产生一个systemExit异常,可以为此做些清除除理的工作。
这个可选参数默认正常退出状态是0,以数值为参数的范围为:0-127。其他的数值为非正常退出,
还有另一种类型,在这里展现的是strings对象类型。
如果准备在退出前自己清理一些东西(比如删除临时文件), 
你可以配置一个 "退出处理函数"(exit handler), 它将在程序退出的时候自动被调用。。
另一种捕获sys.exit调用的方法:
exit_handler.py文件:

#!usr/bin/env python#coding:utf-8import sysdef exitfunc():    print 'exit is done!'sys.exitfunc=exitfunc # 设置退出时要自动调用的函数print 'hello world'sys.exit(1) # 退出,后边的语句不再执行( 但在退出之前会自动调用exitfunc() )print 'ok'  #该语句不会执行
运行结果:
song@ubuntu:~$ python exit_handler.py
hello world
exit is done!


(完)


1 0