码迷,mamicode.com
首页 > 编程语言 > 详细

python:sys模块整理

时间:2020-01-31 19:12:21      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:osi   交互模式   ecs   test   参数   解释   信息   ini   data   

  python的sys模块为开发者提供了访问由解释器使用或维护的接口,并提供了一些函数用来和解释器进行交互,操控python的运行环境

  下面将介绍sys模块常用的属性和方法

1、动态对象

(1)sys.argv列表

  功能:在程序外部(命令行)向内部传递参数;其中sys.argv[0]是脚本名称

  脚本:test.py

import sys

print("sys.argv[0]")
print("sys.argv[1]")


>>python test.py argv
test.py
argv

(2)sys.path列表

  功能:模块的搜索路径;sys.path[0]表示当前脚本所在目录,在交互模式下sys.path[0]为空字符;自定义脚本放在sys.path列表中的任何一个目录下就可以通过import导入使用

  脚本:test.py

import sys

print(sys.path)


>>python test.py
[/box, /usr/local/python-3.6.0/lib/python36.zip, /usr/local/python-3.6.0/lib/python3.6, /usr/local/python-3.6.0/lib/python3.6/lib-dynload, /usr/local/python-3.6.0/lib/python3.6/site-packages]

(3)sys.modules字典

  功能:一个全局字典,python启动后就将该字典加载到内存中,每导入一个新模块,都会被记录在这个字典中,第二次导入相同模块时,解释器将直接查找字典,从而加快程序运行的速度

  脚本:test.py

import sys

print(sys.modules)


>>python test.py
{builtins: <module builtins (built-in)>, sys: <module sys (built-in)>, _frozen_importlib: <module _frozen_importlib (frozen)>, _imp: <module _imp (built-in)>, _warnings: <module _warnings (built-in)>, _thread: <module _thread (built-in)>, _weakref: <module _weakref (built-in)>, _frozen_importlib_external: <module _frozen_importlib_external (frozen)>, _io: <module io (built-in)>, marshal: <module marshal (built-in)>, posix: <module posix (built-in)>, zipimport: <module zipimport (built-in)>, encodings: <module encodings from /usr/local/python-3.6.0/lib/python3.6/encodings/__init__.py>, codecs: <module codecs from /usr/local/python-3.6.0/lib/python3.6/codecs.py>, _codecs: <module _codecs (built-in)>, encodings.aliases: <module encodings.aliases from /usr/local/python-3.6.0/lib/python3.6/encodings/aliases.py>, encodings.utf_8: <module encodings.utf_8 from /usr/local/python-3.6.0/lib/python3.6/encodings/utf_8.py>, _signal: <module _signal (built-in)>, __main__: <module __main__ from main.py>, encodings.latin_1: <module encodings.latin_1 from /usr/local/python-3.6.0/lib/python3.6/encodings/latin_1.py>, io: <module io from /usr/local/python-3.6.0/lib/python3.6/io.py>, abc: <module abc from /usr/local/python-3.6.0/lib/python3.6/abc.py>, _weakrefset: <module _weakrefset from /usr/local/python-3.6.0/lib/python3.6/_weakrefset.py>, _bootlocale: <module _bootlocale from /usr/local/python-3.6.0/lib/python3.6/_bootlocale.py>, _locale: <module _locale (built-in)>, site: <module site from /usr/local/python-3.6.0/lib/python3.6/site.py>, os: <module os from /usr/local/python-3.6.0/lib/python3.6/os.py>, errno: <module errno (built-in)>, stat: <module stat from /usr/local/python-3.6.0/lib/python3.6/stat.py>, _stat: <module _stat (built-in)>, posixpath: <module posixpath from /usr/local/python-3.6.0/lib/python3.6/posixpath.py>, genericpath: <module genericpath from /usr/local/python-3.6.0/lib/python3.6/genericpath.py>, os.path: <module posixpath from /usr/local/python-3.6.0/lib/python3.6/posixpath.py>, _collections_abc: <module _collections_abc from /usr/local/python-3.6.0/lib/python3.6/_collections_abc.py>, _sitebuiltins: <module _sitebuiltins from /usr/local/python-3.6.0/lib/python3.6/_sitebuiltins.py>, sysconfig: <module sysconfig from /usr/local/python-3.6.0/lib/python3.6/sysconfig.py>, _sysconfigdata_m_linux_x86_64-linux-gnu: <module _sysconfigdata_m_linux_x86_64-linux-gnu from /usr/local/python-3.6.0/lib/python3.6/_sysconfigdata_m_linux_x86_64-linux-gnu.py>}

(4)sys.exc_info()方法

  功能:返回值为一个包含异常类型、异常实例和异常回溯信息的元组

  脚本:test.py

import sys

a = "abc"

try:
    int(a)
except:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print(exc_type)
    print(exc_value)
    print(exc_traceback)
    

>>python test.py
<class ValueError>
invalid literal for int() with base 10: abc
<traceback object at 0x7feb61a92dc8>

(5)sys.last_type

  功能:返回最近一次捕获的异常的类型,只在交互式环境下可用

(6)sys.last_value

  功能:返回最近一次捕获的异常的值,只在交互式环境下可用

(7)sys.last_traceback

  功能:返回最近一次捕获的异常的追踪信息,只在交互式环境下可用

python:sys模块整理

标签:osi   交互模式   ecs   test   参数   解释   信息   ini   data   

原文地址:https://www.cnblogs.com/xmcwm/p/12246141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!