python基础课程_学习笔记13:标准库:有些收藏夹——sys

标准库:有些收藏夹

sys

sys这个模块可以让你访问和python解释器联系紧密的变量和函数

sys模块中一些重要的函数和变量

函数/变量 描写叙述

argv 命令行參数,包含脚本名称

exit([arg]) 退出当前程序,可选參数为给定的返回值或者错误信息

modules 映射模块名字到加载模块的字典

path 查找模块所在文件夹的文件夹名列表

platform 类似sunos5或者win32的平台标识符

stdin 标准输入流——一个类文件对象

stdout 标准输出流——一个类文件对象

stderr 标准错误流——一个类文件对象

变量sys.argv包含传递到python解释器的參数,包含脚本名称;

函数sys.exit能够退出当前程序(假设在try/finally块中调用。finally子句的内容仍然会被执行)。

能够提供一个整数參数。用来标识程序是否成功执行——unix的一个惯例。大多数情况下使用该整数的默认值就能够了(也就是0。表示成功)。

或者也能够提供字符串參数,用作错误信息,这对于用户找出程序停止执行的原因会非常实用。

这样,程序就会在退出的时候提供错误信息和标识程序执行失败的代码。

映射sys.modules将模块名映射到实际存在的模块上,它仅仅应用于眼下导入的模块。

sys.path是一个字符串列表,当中的每一个字符串都是一个文件夹名,在import语句运行时。解释器就会从这些文件夹中查找模块。

sys.platform模块变量是解释器正在其上执行的“平台”名称。

可能是操作系统的名字。也可能是标识其它种类的平台,假设你执行Jython的话,就是java虚拟机。

sys.stdinsys.stdoutsys.stderr模块变量是类文件流对象。它们表示标准UNIX概念中的标准输入、标准输出和标准错误。

练习 处理命令行參数

当通过命令行调用python脚本时,可能会在后面加上一些參数——它们就是命令行參数。这些參数会放置在sys.argv列表中,脚本的名字为sys.argv[0]

$ cat reverseargs.py

#!/usr/bin/python

import sys

args=sys.argv[1:]

args.reverse()

print ' '.join(args)

$ ./reverseargs.py 1 3 4

4 3 1

$ ./reverseargs.py 1 3 "ab" [2,4,'6']

6 4 2 ab 3 1

获取脚本參数

脚本内容

$ cat sys-argv-example-1.py

# File:sys-argv-example-1.py

import sys

print "Script name is",sys.argv[0]

if len(sys.argv) > 1:

        print "there is",len(sys.argv)-1,"arguments:"

        print [x for x in sys.argv[1:]]

else:

        print "there are no arguments!"

运行结果

$ python sys-argv-example-1.py

Script name is sys-argv-example-1.py

there are no arguments!

$ python sys-argv-example-1.py 'a' 2 'c' 'four'

Script name is sys-argv-example-1.py

there is 4 arguments:

['a', '2', 'c', 'four']

假设从标准输入读取脚本(如”python < sys-argv-example-1.py”),脚本名称将被设置为空字符串。

假设把脚本作为字符串传递给python(使用-c參数)。则脚本名称被设置为“-c”。

$ python < sys-argv-example-1.py

Script name is

there are no arguments!

练习 处理模块

操作模块的路径

脚本内容

$ cat sys-path-example-1.py

#File:sys-path-example-1.py

import sys

print "path has",len(sys.path),"members"

print sys.path

#add the sample directory to the path

sys.path.insert(0,"samples")

print "path has",len(sys.path),"members"

print sys.path

import sample

#nuke the path

sys.path=[]

import random # oops!

运行结果

path has 10 members

['/home/ggz2/magiccube/mysh/pys', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages']

path has 11 members

['samples', '/home/ggz2/magiccube/mysh/pys', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages']

Traceback (most recent call last):

  File "sys-path-example-1.py", line 12, in <module>

    import sample

ImportError: No module named sample

查找内建模块

脚本内容

$ cat sys-builtin-module-names-example-1.py

# File :sys-builtin-module-names-example-1.py

import sys

def dump(module):

        print module,"=>",

        if module in sys.builtin_module_names:

                print "<BUILTIN>"

        else:

                module = __import__(module)

                print module.__file__

dump("os")

dump("sys")

dump("string")

dump("strop")

dump("zlib")

运行结果

$ python sys-builtin-module-names-example-1.py

os => /usr/lib64/python2.6/os.pyc

sys => <BUILTIN>

string => /usr/lib64/python2.6/string.pyc

strop => /usr/lib64/python2.6/lib-dynload/stropmodule.so

zlib => /usr/lib64/python2.6/lib-dynload/zlibmodule.so

查找导入的模块

脚本内容

$ cat sys-modules-example-1.py

#File:sys-modules-example-1.py

import sys

print sys.modules.keys()

运行结果

$ python sys-modules-example-1.py

['copy_reg', 'encodings', 'site', '__builtin__', '__main__', 'encodings.encodings', 'abc', 'posixpath', 'errno', 'encodings.codecs', '_abcoll', 'types', '_codecs', '_warnings', 'genericpath', 'stat', 'zipimport', 'encodings.__builtin__', 'warnings', 'UserDict', 'encodings.utf_8', 'sys', 'codecs', 'os.path', 'signal', 'linecache', 'posix', 'encodings.aliases', 'exceptions', 'os']

练习 引用计数

获得引用数量

getrefcount函数返回给定对象的引用数量。即变量被引用的数量。python追踪这些变量,当引用数为0时,对象被销毁。

脚本内容

$ cat sys-getrefcount-example-1.py

#File:sys-getrefcount-example-1.py

import sys

variable=1234

print sys.getrefcount(0)

print sys.getrefcount(variable)

print sys.getrefcount(None)

运行结果

$ python sys-getrefcount-example-1.py

147

4

700

注意:这个值总是比实际的值要大些,由于当确定值时函数本身依赖这个对象。

练习 主机平台

脚本内容

$ cat sys-platform-example-1.py

#File:sys-platform-example-1.py

import sys

#

# emulate "import os.path" (sort of)...

if sys.platform == "win32":

        import ntpath

        pathmodule=ntpath

elif sys.platform == "mac":

        import macpath

        pathmodule=macpath

else:

        # assume it's a posix platform

        import posixpath

        pathmodule=posixpath

print pathmodule

运行结果

centos 5.4系统下运行:

$ python sys-platform-example-1.py

<module 'posixpath' from '/usr/lib64/python2.6/posixpath.pyc'>

Win7系统下运行:

<module 'ntpath' from 'D:software(x86)Python27lib tpath.pyc'>

典型的平台名称为win32Windows 9x/NT平台)和macMacintosh平台)。Unix操作系统,平台名称通常由命令“uname -r”命令导出。比方irix6linux2,或者sunos5Solaris)。

$ uname -r

2.6.32-358.el6.x86_64

练习 追踪程序

setprofiler函数同意你配置一个配置函数。每当一个函数或方法被调用,每一次返回(隐式或显式),每一次异常时被调用。

脚本内容

$ cat sys-setprofiler-example-1.py

#File:sys-setprofiler-example-1.py

import sys

def test(n):

        j=0

        for i in range(n):

                j=j+i

        return n

def profiler(frame,event,arg):

        print event,frame.f_code.co_name,frame.f_lineno,"->",arg

# profiler is activated on the next call,return,or exception

sys.setprofile(profiler)

# profile this function call

test(1)

# disable profiler

sys.setprofile(None)

# don't profile this call

test(2)

运行结果

$ python sys-setprofiler-example-1.py

call test 5 -> None

c_call test 7 -> <built-in function range>

c_return test 7 -> <built-in function range>

return test 9 -> 1

c_call <module> 21 -> <built-in function setprofile>

基于该函数,profile模块提供了完整的分析器框架。

settrace函数与此类似,但trace函数在解释器运行新行的时候被调用。

脚本内容

$ cat sys-settrace-example-1.py

#File:sys-settrace-example-1.py

import sys

def test(n):

        j=0

        for i in range(n):

                j=j+1

        return n

def tracer(frame,event,arg):

        print event,frame.f_code.co_name,frame.f_lineno,"->",arg

        return tracer

#trace is activated on the next call,return,or exception

sys.settrace(tracer)

#trace this function call

test(1)

# disable tracing

sys.settrace(None)

# don't trace this call

test(2)

运行结果

$ python sys-settrace-example-1.py

call test 5 -> None

line test 6 -> None

line test 7 -> None

line test 8 -> None

line test 7 -> None

line test 9 -> None

return test 9 -> 1

基于该函数提供的跟踪功能,pdb模块提供了一个完整的调试器框架。

练习 操作标准输入输出

stdinstdoutstderr变量包括与标准输入输出流相相应的流对象。假设须要提供比print更好地控制输出能够直接訪问它们。

假设你想重定向输入和输出到其他设备也能够替换它们。或者以某种非标准方式处理它们。

重定向输出

脚本内容

$ cat sys-stdout-example-1.py

#File:sys-stdout-example-1.py

import sys

import string

class Redirect:

        def __init__(self,stdout):

                self.stdout=stdout

        def write(self,s):

                self.stdout.write(string.lower(s))

# redirect standard output(including the print statement)

old_stdout=sys.stdout

sys.stdout=Redirect(sys.stdout)

print "HEJA SVERIGE",

print "FRISKT HUM303226R"

# restore standard output

sys.stdout=old_stdout

print "M303205303205303205303205L!"

运行结果

$ python sys-stdout-example-1.py

heja sverige friskt humÖr

MÅÅÅÅL!

练习 退出程序

当到达主程序的末尾时,解释器自己主动终止。假设须要中途退出,能够调用sys.exit函数。它能给调用它的程序返回一个可配置的整数。

退出程序

脚本内容

$ cat sys-exit-example-1.py

#File :sys-exit-example-1.py

import sys

print "Hello"

sys.exit(1)

print "there"

运行结果

$ python sys-exit-example-1.py

Hello

注意:sys.exit不是马上退出的,相反地。它引发了一个SystemExit异常。

捕获调用sys.exit

脚本内容

$ cat sys-exit-example-2.py

#File :sys-exit-example-2.py

import sys

print "Hello"

try:

        sys.exit(1)

except SystemExit:

        pass

print "there"

运行结果

$ python sys-exit-example-2.py

Hello

there

假设你想在你之后清空一些内容。能够设置exit handle,它是一个在退出时自己主动调用的函数。

用还有一种方式捕获调用sys.exit

脚本内容

$ cat sys-exitfunc-example-1.py

#File:sys-exitfunc-example-1.py

import sys

def exitfunc():

        print "world"

sys.exitfunc=exitfunc

print "hello"

sys.exit(1)

print "there" # never printed

运行结果

$ python sys-exitfunc-example-1.py

hello

world

版权声明:本文博主原创文章,博客,未经同意不得转载。

原文地址:https://www.cnblogs.com/bhlsheji/p/4883443.html