记录一下我使用过的sys模块(将会不断更新)
1.系统相关
1.1 sys.path
返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
>>> import sys
>>> print sys.path
['', '/opt/stack/keystone', '/opt/stack/cinder', '/opt/stack/nova', '/opt/stack/glance', '/opt/stack/horizon', '/home/stephen/openstack/demo/blog-dashboard', '/home/stephen/openstack/demo/blog', '/usr/local/lib/python2.7/dist-packages/clidemo-0.1-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/cli-0.1-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/cliffdemo-0.1-py2.7.egg', '/home/stephen/openstack/demo/pecan_test', '/home/stephen/highlander/kilo/highlander-agent', '/usr/local/lib/python2.7/dist-packages/python_daemon-2.1.0-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/lockfile-0.12.2-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/docutils-0.12-py2.7.egg', '/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/local/lib/python2.7/dist-packages/PIL', '/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/python2.7/dist-packages/ubuntu-sso-client']
1.2 sys.platform
返回操作系统平台名称
>>> import sys
>>> print sys.platform
linux2
>>>
1.3 sys.executable
Python解释程序路径
>>> import sys
>>> print sys.executable
/usr/bin/python
>>>
2. 模块信息
2.1 sys.modules.keys()
返回所有已经导入的模块列表
>>> import sys
>>> print sys.modules.keys()
['copy_reg', 'sre_compile', '_sre', 'encodings', 'site', '__builtin__', 'sysconfig', '__main__', 'encodings.encodings', 'repoze.who', 'repoze.who.plugins', 'abc', 'posixpath', '_weakrefset', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'types', 'dogpile', 'encodings.__builtin__', '_warnings', 'wsmeext', 'genericpath', 'stat', 'zipimport', '_sysconfigdata', 'warnings', 'UserDict', 'encodings.utf_8', 'sys', 'repoze', 'readline', '_sysconfigdata_nd', 'xstatic.pkg', 'os.path', 'codecs', 'sitecustomize', 'signal', 'traceback', 'paste', '_codecs', 'xstatic', 'linecache', 'posix', 'encodings.aliases', 'exceptions', 'sre_parse', 'os', '_weakref']
>>>
2.2 sys.modules
返回系统导入的模块字段,key是模块名,value是模块
2.3 sys.builtin_module_names
Python解释器导入的模块列表
3. 脚本相关
3.1 sys.argv
所有命令行参数都存放在List中,是一个可迭代对象,第一个元素sys.argv[0]是程序本身[非全路径]
#vim test.py
#!/bin/env python
import sys
print sys.argv
for argv in enumerate(sys.argv):
print argv
sys.exit(99)
#没有参数的情况下运行
# ~$./test.py
['/root/test.py']
(0, '/root/test.py')
#############################
#有参数的情况下运行
# ~$./test.py you are 1th
['/root/test.py','you','are','1th']
(0, '/root/test.py')
(1, 'you')
(2, 'are')
(3, '1th')
3.2 sys.exit(n)
退出程序,正常退出时exit(0),定义退出状态。使用sys.exit(n)退出程序将会抛出SystemExit的异常,可以捕获该异常为程序的推出做一些清场处理。
4. 输入输出相关
4.1 sys.stdout, sys.stdin, sys.stderr
sys.stdout:标准输出, sys.stdin:标准输入 sys.stderr:错误输出
>>> import sys
>>> for f in (sys.stdin, sys.stdout, sys.stderr):
... print f
...
<open file '<stdin>', mode 'r' at 0x7f425cc0b0c0>
<open file '<stdout>', mode 'w' at 0x7f425cc0b150>
<open file '<stderr>', mode 'w' at 0x7f425cc0b1e0>
>>>
由此可以看出stdin, stdout, stderr在Python中都是文件属性的对象,他们在Python启动时自动与Shell 环境中的标准输入,输出,出错关联。
>>> dir(sys.stdout)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']
>>>
>>> dir(sys.stdin)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']
>>>
>>> dir(sys.stderr)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']
>>>
他们拥有的属性都一样。
可以看到这些方法和文件对象几乎一样
sys.stdout.write 无换行符输出,在需要无换行符输出的时候需要用到
sys.stdout.writelines() 无换行符输出
sys.stdout.write(“a”) 屏幕输出无换行符a
sys.stdout.writelines(‘a’)
需要需要换行,需要\n
sys.stdin.readline() 从标准输入读一行,和input和raw_input()有点类似
4.2 sys.exc_info()
函数返回的三个值:类型(异常类),值(异常实例),和回溯(traceback),相应的回溯对象.获取当前正在处理的异常类,exc_type、exc_value、exc_traceback当前处理的异常详细信息
5. 其他
sys.hexversion 获取Python解释程序的版本值,16进制格式如:0x020403F0
sys.version 获取Python解释程序的版本信息
sys.maxint 最大的Int值
sys.maxunicode 最大的Unicode值
sys.exc_clear() 用来清除当前线程所出现的当前的或最近的错误信息
sys.exec_prefix 返回平台独立的python文件安装的位置
sys.byteorder 本地字节规则的指示器,big-endian平台的值是’big’,little-endian平台的值是’little’
sys.copyright 记录python版权相关的东西
sys.api_version 解释器的C的API版本
sys.version_info ‘final’表示最终,也有’candidate’表示候选,表示版本级别,是否有后继的发行
sys.displayhook(value) 如果value非空,这个函数会把他输出到sys.stdout,并且将他保存进builtin..指在python的交互式解释器里,’’ 代表上次你输入得到的结果,hook是钩子的意思,将上次的结果钩过来
sys.getdefaultencoding() 返回当前你所用的默认的字符编码格式
sys.getfilesystemencoding() 返回将Unicode文件名转换成系统文件名的编码的名字
sys.setdefaultencoding(name)用来设置当前默认的字符编码,如果name和任何一个可用的编码都不匹配,抛出 LookupError,这个函数只会被site模块的sitecustomize使用,一旦别site模块使用了,他会从sys模块移除
sys.builtin_module_names Python解释器导入的模块列表
sys.executable Python解释程序路径
sys.getwindowsversion() 获取Windows的版本