zoukankan      html  css  js  c++  java
  • python 解释器交互模块 -- sys

    sys模块是与python解释器交互的一个接口

    sys.argv 命令行参数List,第一个元素是程序本身路径
    sys.getdefaultencoding(): 获取系统当前编码,一般默认为ascii。
    sys.setdefaultencoding(): 设置系统默认编码,执行dir(sys)时不会看到这个方法,在解释器中执行不通过,可以先执行reload(sys),在执行 setdefaultencoding('utf8'),此时将系统默认编码设置为utf8。(见设置系统默认编码 )。
    sys.getfilesystemencoding(): 获取文件系统使用编码方式,Windows下返回'mbcs',mac下返回'utf-8'.
    sys.modules.keys() 返回所有已经导入的模块列表
    sys.exc_info() 获取当前正在处理的异常类,exc_type、exc_value、exc_traceback当前处理的异常详细信息
    sys.exit(n) 退出程序,正常退出时exit(0)
    sys.hexversion 获取Python解释程序的版本值,16进制格式如:0x020403F0
    sys.version 获取Python解释程序的版本信息
    sys.maxint 最大的Int值
    sys.maxunicode 最大的Unicode值
    sys.modules 返回系统导入的模块字段,key是模块名,value是模块
    sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
    sys.platform 返回操作系统平台名称
    sys.stdout 标准输出
    sys.stdin 标准输入
    sys.stderr 错误输出
    sys.exc_clear() 用来清除当前线程所出现的当前的或最近的错误信息
    sys.exec_prefix 返回平台独立的python文件安装的位置
    sys.byteorder 本地字节规则的指示器,big-endian平台的值是'big',little-endian平台的值是'little'
    sys.copyright 记录python版权相关的东西
    sys.api_version 解释器的C的API版本

    1,argv : 处理命令行参数

    在解释器启动后, argv 列表包含了传递给脚本的所有参数, 列表的第一个元素为脚本自身的名称.

    import sys
    print(sys.argv[0])
    
    结果:D:/pythonfile/PyCharm/week6/da.py

    2,path : 处理模块

    path 列表是一个由目录名构成的列表, Python 从中查找扩展模块( Python 源模块, 编译模块,或者二进制扩展).

    启动 Python 时,这个列表从根据内建规则, PYTHONPATH 环境变量的内容, 以及注册表( Windows 系统)等进行初始化.

    由于它只是一个普通的列表, 你可以在程序中对它进行操作,

    import sys
    
    print(sys.path)
    
    结果 : ['D:\pythonfile\PyCharm\week6', 'D:\pythonfile\PyCharm', 'D:\pythonfile\PyCharm\venv\Scripts\python37.zip', 'C:\python37\Lib', 'C:\python37\DLLs', "C:\Users\deng'peng\AppData\Local\Programs\Python\Python37-32", 'D:\pythonfile\PyCharm\venv', 'D:\pythonfile\PyCharm\venv\lib\site-packages', 'D:\pythonfile\PyCharm\venv\lib\site-packages\setuptools-39.1.0-py3.7.egg', 'D:\pythonfile\PyCharm\venv\lib\site-packages\pip-10.0.1-py3.7.egg', 'D:\软件\PyCharm 2018.1.4\helpers\pycharm_matplotlib_backend']

    3,modules : 查找已导入的模块

    全局字典,每当python启动时该字段自动加载到内存中。新加模块sys.modules会自动记录该模块,第二次导入时直接从字典中加载,加快运行速度。他拥有字典的一切方法。
    keys是模块名
    values是模块
    modules返回路径

    import sys
    
    
    print(sys.modules)
    print("``````````````")
    print(sys.modules.keys())
    print("``````````````")
    print(sys.modules.values())
    print("```````````````````")
    print(sys.modules["os"]
    
    运行结果:
    {'sys': <module 'sys' (built-in)>, 'builtins': <module 'builtins' (built-in)>, '_frozen_importlib': <module '_frozen_importlib' (frozen)>, '_imp': <module '_imp' (built-in)>, '_thread': <module '_thread' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_weakref': <module '_weakref' (built-in)>, 'zipimport': <module 'zipimport' (built-in)>, '_frozen_importlib_external': <module '_frozen_importlib_external' (frozen)>, '_io': <module 'io' (built-in)>, 'marshal': <module 'marshal' (built-in)>, 'nt': <module 'nt' (built-in)>, 'winreg': <module 'winreg' (built-in)>, 'encodings': <module 'encodings' from 'C:\python37\Lib\encodings\__init__.py'>, 'codecs': <module 'codecs' from 'C:\python37\Lib\codecs.py'>, '_codecs': <module '_codecs' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from 'C:\python37\Lib\encodings\aliases.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from 'C:\python37\Lib\encodings\utf_8.py'>, '_signal': <module '_signal' (built-in)>, '__main__': <module '__main__' from 'D:/pythonfile/PyCharm/week6/day34.py'>, 'encodings.latin_1': <module 'encodings.latin_1' from 'C:\python37\Lib\encodings\latin_1.py'>, 'io': <module 'io' from 'C:\python37\Lib\io.py'>, 'abc': <module 'abc' from 'C:\python37\Lib\abc.py'>, '_abc': <module '_abc' (built-in)>, 'site': <module 'site' from 'C:\python37\Lib\site.py'>, 'os': <module 'os' from 'C:\python37\Lib\os.py'>, 'stat': <module 'stat' from 'C:\python37\Lib\stat.py'>, '_stat': <module '_stat' (built-in)>, 'ntpath': <module 'ntpath' from 'C:\python37\Lib\ntpath.py'>, 'genericpath': <module 'genericpath' from 'C:\python37\Lib\genericpath.py'>, 'os.path': <module 'ntpath' from 'C:\python37\Lib\ntpath.py'>, '_collections_abc': <module '_collections_abc' from 'C:\python37\Lib\_collections_abc.py'>, '_sitebuiltins': <module '_sitebuiltins' from 'C:\python37\Lib\_sitebuiltins.py'>, '_bootlocale': <module '_bootlocale' from 'C:\python37\Lib\_bootlocale.py'>, '_locale': <module '_locale' (built-in)>, 'encodings.gbk': <module 'encodings.gbk' from 'C:\python37\Lib\encodings\gbk.py'>, '_codecs_cn': <module '_codecs_cn' (built-in)>, '_multibytecodec': <module '_multibytecodec' (built-in)>, 'encodings.cp437': <module 'encodings.cp437' from 'C:\python37\Lib\encodings\cp437.py'>, 'sitecustomize': <module 'sitecustomize' from 'D:\软件\PyCharm 2018.1.4\helpers\pycharm_matplotlib_backend\sitecustomize.py'>, 'time': <module 'time' (built-in)>, 'random': <module 'random' from 'C:\python37\Lib\random.py'>, 'warnings': <module 'warnings' from 'C:\python37\Lib\warnings.py'>, 'types': <module 'types' from 'C:\python37\Lib\types.py'>, 'math': <module 'math' (built-in)>, 'hashlib': <module 'hashlib' from 'C:\python37\Lib\hashlib.py'>, '_hashlib': <module '_hashlib' from 'C:\python37\DLLs\_hashlib.pyd'>, '_blake2': <module '_blake2' (built-in)>, '_sha3': <module '_sha3' (built-in)>, 'itertools': <module 'itertools' (built-in)>, 'bisect': <module 'bisect' from 'C:\python37\Lib\bisect.py'>, '_bisect': <module '_bisect' (built-in)>, '_random': <module '_random' (built-in)>}
    ``````````````
    dict_keys(['sys', 'builtins', '_frozen_importlib', '_imp', '_thread', '_warnings', '_weakref', 'zipimport', '_frozen_importlib_external', '_io', 'marshal', 'nt', 'winreg', 'encodings', 'codecs', '_codecs', 'encodings.aliases', 'encodings.utf_8', '_signal', '__main__', 'encodings.latin_1', 'io', 'abc', '_abc', 'site', 'os', 'stat', '_stat', 'ntpath', 'genericpath', 'os.path', '_collections_abc', '_sitebuiltins', '_bootlocale', '_locale', 'encodings.gbk', '_codecs_cn', '_multibytecodec', 'encodings.cp437', 'sitecustomize', 'time', 'random', 'warnings', 'types', 'math', 'hashlib', '_hashlib', '_blake2', '_sha3', 'itertools', 'bisect', '_bisect', '_random'])
    ``````````````
    dict_values([<module 'sys' (built-in)>, <module 'builtins' (built-in)>, <module '_frozen_importlib' (frozen)>, <module '_imp' (built-in)>, <module '_thread' (built-in)>, <module '_warnings' (built-in)>, <module '_weakref' (built-in)>, <module 'zipimport' (built-in)>, <module '_frozen_importlib_external' (frozen)>, <module 'io' (built-in)>, <module 'marshal' (built-in)>, <module 'nt' (built-in)>, <module 'winreg' (built-in)>, <module 'encodings' from 'C:\python37\Lib\encodings\__init__.py'>, <module 'codecs' from 'C:\python37\Lib\codecs.py'>, <module '_codecs' (built-in)>, <module 'encodings.aliases' from 'C:\python37\Lib\encodings\aliases.py'>, <module 'encodings.utf_8' from 'C:\python37\Lib\encodings\utf_8.py'>, <module '_signal' (built-in)>, <module '__main__' from 'D:/pythonfile/PyCharm/week6/day34.py'>, <module 'encodings.latin_1' from 'C:\python37\Lib\encodings\latin_1.py'>, <module 'io' from 'C:\python37\Lib\io.py'>, <module 'abc' from 'C:\python37\Lib\abc.py'>, <module '_abc' (built-in)>, <module 'site' from 'C:\python37\Lib\site.py'>, <module 'os' from 'C:\python37\Lib\os.py'>, <module 'stat' from 'C:\python37\Lib\stat.py'>, <module '_stat' (built-in)>, <module 'ntpath' from 'C:\python37\Lib\ntpath.py'>, <module 'genericpath' from 'C:\python37\Lib\genericpath.py'>, <module 'ntpath' from 'C:\python37\Lib\ntpath.py'>, <module '_collections_abc' from 'C:\python37\Lib\_collections_abc.py'>, <module '_sitebuiltins' from 'C:\python37\Lib\_sitebuiltins.py'>, <module '_bootlocale' from 'C:\python37\Lib\_bootlocale.py'>, <module '_locale' (built-in)>, <module 'encodings.gbk' from 'C:\python37\Lib\encodings\gbk.py'>, <module '_codecs_cn' (built-in)>, <module '_multibytecodec' (built-in)>, <module 'encodings.cp437' from 'C:\python37\Lib\encodings\cp437.py'>, <module 'sitecustomize' from 'D:\软件\PyCharm 2018.1.4\helpers\pycharm_matplotlib_backend\sitecustomize.py'>, <module 'time' (built-in)>, <module 'random' from 'C:\python37\Lib\random.py'>, <module 'warnings' from 'C:\python37\Lib\warnings.py'>, <module 'types' from 'C:\python37\Lib\types.py'>, <module 'math' (built-in)>, <module 'hashlib' from 'C:\python37\Lib\hashlib.py'>, <module '_hashlib' from 'C:\python37\DLLs\_hashlib.pyd'>, <module '_blake2' (built-in)>, <module '_sha3' (built-in)>, <module 'itertools' (built-in)>, <module 'bisect' from 'C:\python37\Lib\bisect.py'>, <module '_bisect' (built-in)>, <module '_random' (built-in)>])
    ```````````````````
    <module 'os' from 'C:\python37\Lib\os.py'>

    4,platform : 获得当前平台

    sys.platform  返回当前平台 出现如: "win32" "linux2" 等

    print(sys.platform)
    
    结果:win32

    5,exit : 退出程序

    如果准备在退出前自己清理一些东西(比如删除临时文件), 你可以配置一个 "退出处理函数"(exit handler), 它将在程序退出的时候自动被调用

    import sys
    print("hello")
    try:
        sys.exit(1)
    except SystemExit: # 捕获退出的异常
        pass           # 捕获后不做任何操作
    print("There")
    
    结果:
    hello
    There



  • 相关阅读:
    Linux常用命令-centos
    USACO 2006 Open, Problem. The Country Fair 动态规划
    USACO 2007 March Contest, Silver Problem 1. Cow Traffic
    USACO 2007 December Contest, Silver Problem 2. Building Roads Kruskal最小生成树算法
    USACO 2015 February Contest, Silver Problem 3. Superbull Prim最小生成树算法
    LG-P2804 神秘数字/LG-P1196 火柴排队 归并排序, 逆序对
    数据结构 并查集
    浴谷国庆集训 对拍
    1999 NOIP 回文数
    2010 NOIP 普及组 第3题 导弹拦截
  • 原文地址:https://www.cnblogs.com/peng104/p/9588491.html
Copyright © 2011-2022 走看看