Python入门(二十一)- 常见模块

二十一、常见模块

上一章介绍了Python模块的相关知识,在实际开发中,Python的很多功能都已经有了成熟的第三方实现,一般不需要开发者”重复造轮子“,当开发者需要完成某种功能时,通过搜索引擎进行搜索,通常可以找到第三方在Python中为该功能所提供的扩展模块。实际上,Python语言本身也内置了大量模块,对于常规的日期、时间、正则表达式、JSON支持、容器类等,Python内置的模块已经非常完备。
Python内置模块总是在不断更新中,本文只是起到抛砖引玉的作用

21.1 常用模块

21.1.1 sys模块

sys模块代表了Python解释器,主要用于获取和Python解释器相关的信息。
如下示例,查看sys模块包含的程序单元(包括变量、函数等)。

>>> import sys
>>> [e for e in dir(sys) if not e.startswith('_')]
['addaudithook', 'api_version', 'argv', 'audit', 'base_exec_prefix', 'base_prefix', 
'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'copyright', 
'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 
'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 
'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'getallocatedblocks', 
'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 
'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 
'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern',
 'is_finalizing', 'last_traceback', 'last_type', 'last_value', 'maxsize', 'maxunicode',
  'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 
  'platlibdir', 'prefix', 'ps1', 'ps2', 'pycache_prefix', 'set_asyncgen_hooks', 
  'set_coroutine_origin_tracking_depth', 'setprofile', 'setrecursionlimit', 
  'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 
  'unraisablehook', 'version', 'version_info', 'warnoptions', 'winver']
>>>

不必记住所有的单元,通常时用到哪些模块就去查阅其对应的说明文档和参考手册。sys模块的参考页面.
大部分时候用不到sys模块里很冷僻的功能,本节只介绍sys模块中常用属性和函数。

  • sys.argv : 获取运行Python程序的命令行参数。其中sys.argv[0]通常指该Python程序,sys.argv[1]代表Python程序提供的第一个参数,sys.argv[2]代表第二个参数…以此类推。
  • sys.byteorder : 显示本地字节序的指示符。如果本地字节序时大端模式。则该属性返回big;否则返回little。
  • sys.copyright : 该属性返回与Python解释器相关的版权信息。
  • sys.executable : 该属性返回Python解释器在磁盘上的存储路径。
  • sys.flags : 该只读属性返回运行Python命令时指定的旗标。
  • sys.getfilesystemencoding() : 返回在当前系统中保存文件所用的字符集。
  • sys.getrefcount(object) : 返回指定对象的引用计算。前面介绍过,当object对象的引用计算为0时,系统会回收该对象。
  • sys.getrecursionlimit() : 返回Python解释器当前支持的递归深度,该属性可通过setrecursionlimit()方法重置。
  • sys.getswitchinterval() : 返回在当前Python解释器中线程切换的时间间隔,该属性可以通过setswitchinterval()函数改变。
  • sys.implementation:返回当前Python解释器的实现。
  • sys.maxsize: 返回Python整数支持的最大值。在32位平台上,该属性值位231-1;在64位平台上,该属性值为263-1.
  • sys.modules : 返回模块名和载入模块对应关系的字典。
  • sys.path : 该属性指定Python产找模块的路径列表。程序可通过修改该属性来动态增加Python加载模块的路径。
  • sys.platform : 返回Python解释器所在平台的标识符。
  • sys.stdin : 返回系统的标准输入流(类文件对象)
  • sys.stdout : 返回系统的标准输出流(类文件对象)
  • sys.stderr : 返回系统的错误输出流(类文件对象)
  • sys.version : 返回当前Python解释器的版本信息。
  • sys.winver : 返回当前Python解释器的主版本号。
    示例:
>>> import sys
>>> print(sys.argv)
['']
>>> print(sys.copyright)
Copyright (c) 2001-2021 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
>>> print(sys.executable)
D:\Administrator\05-Personal\Software\python-3.9.9-embed-amd64\python.exe
>>> print(sys.getfilesystemencoding())
utf-8
>>>

21.1.2 os模块

os模块代表了程序所在的操作系统,主要用于获取程序运行所在操作系统的相关信息。
在Python交互解释器中先导入os模块,使用all命令查看该模块对外开放的全部属性和函数。示例如下:

>>> import os
>>> os.__all__
['altsep', 'curdir', 'pardir', 'sep', 'pathsep', 'linesep', 'defpath', 'name', 'path', 
'devnull', 'SEEK_SET', 'SEEK_CUR', 'SEEK_END', 'fsencode', 'fsdecode', 'get_exec_path', 
'fdopen', 'popen', 'extsep', '_exit', 'DirEntry', 'F_OK', 'O_APPEND', 'O_BINARY', 
'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 
'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 
'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'R_OK', 'TMP_MAX', 'W_OK', 'X_OK', 'abort', 'access', 
'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'device_encoding', 'dup', 'dup2', 
'environ', 'error', 'execv', 'execve', 'fspath', 'fstat', 'fsync', 'ftruncate', 
'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 
'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'link', 'listdir', 'lseek', 'lstat', 
'mkdir', 'open', 'pipe', 'putenv', 'read', 'readlink', 'remove', 'rename', 'replace'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值