Python基础学习之标准库sys总结

本篇文章给大家带来了关于python的相关知识,其中主要介绍了标准库sys的相关问题,Python的sys模块提供访问由解释器使用或维护的变量的接口,并提供了一些函数用来和解释器进行交互,操控Python的运行时环境,希望对大家有帮助。

Python基础学习之标准库sys总结

推荐学习:python学习教程

sys作用

Python的sys模块提供访问由解释器使用或维护的变量的接口,并提供了一些函数用来和解释器进行交互,操控Python的运行时环境。

>>> import sys

常用变量

sys.version

返回Python解释器版本号;用于某程序需要用指定版本号运行

>>> import sys
>>> sys.version
3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)]

sys.maxsize

表示操作系统承载的最大int值

>>> import sys
>>> sys.maxsize
9223372036854775807

sys.maxunicode

给出最大Unicode代码点值的整数,即1114111(十六进制0x10FFFF)。

>>> import sys
>>> sys.maxunicode
1114111

sys.path

当前脚本的path环境变量,如果没有python就找到

>>> import sys
>>> sys.path
['', 'D:\\Python3.8\\python38.zip', 'D:\\Python3.8\\DLLs', 'D:\\Python3.8\\lib', 'D:\\Python3.8', 'D:\\Python3.8\\lib\\site-packages']

sys.platform

返回操作系统平台名称,在编写跨平台应用时很有用

系统返回值
Windows‘win32’
Linux‘linux’
Mac‘darwin’
>>> import sys>>> sys.platform
win32

sys.argv

python脚本运行时的脚本名以及参数作为一个list,并输出。实现从程序外部像程序内容传递参数

import sysprint(sys.argv)
E:\Python> python 2.py hello python['2.py', 'hello', 'python']

sys.executable

一个字符串,给出Python解释器的可执行二进制文件的绝对路径。如果Python无法检索其可执行文件的真实路径,sys.executable则将为空字符串或None

>>> import sys
>>> sys.executable
'D:\\Python3.8\\python.exe'

sys.byteorder

本地字节顺序的指示符——在大端序(最高有效位优先)操作系统上值为 ‘big’ ,在小端序(最低有效位优先)操作系统上为 'little

>>> import sys>>> sys.byteorder'little'

sys.version_info

包含版本号的五个组件的元组:major,minor, micro,releaselevel和serial。

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)

sys.api_version

此解释器的C API版本。

>>> import sys>>> sys.api_version1013

sys.stdin/sys.stdout/sys.stderr

标准输入、输出、错误。

>>> import sys
>>> sys.stdin
<_io.TextIOWrapper name=&#39;<stdin>&#39; mode=&#39;r&#39; encoding=&#39;utf-8&#39;>
>>> sys.stdout
<_io.TextIOWrapper name=&#39;<stdout>&#39; mode=&#39;w&#39; encoding=&#39;utf-8&#39;>
>>> sys.stderr
<_io.TextIOWrapper name=&#39;<stderr>&#39; mode=&#39;w&#39; encoding=&#39;utf-8&#39;>

常用方法

sys.exit()

退出程序,正常退出时exit(0)

import sysprint(sys.exit(0))print(sys.exit(1))

sys.modules

返回系统导入的模块字段,key是模块名,value是模块

>>> import sys
>>> sys.modules
{&#39;sys&#39;: <module &#39;sys&#39; (built-in)>, &#39;builtins&#39;: <module &#39;builtins&#39; (built-in)>, &#39;_frozen_importlib&#39;: <module &#39;importlib._bootstrap&#39; (frozen)>, &#39;_imp&#39;: <module &#39;_imp&#39; (built-in)>, &#39;_warnings&#39;: <module &#39;_warnings&#39; (built-in)>, &#39;_frozen_importlib_external&#39;: <module &#39;importlib._bootstrap_external&#39; (frozen)>, &#39;_io&#39;: <module &#39;io&#39; (built-in)>, &#39;marshal&#39;: <module &#39;marshal&#39; (built-in)>, &#39;nt&#39;: <module &#39;nt&#39; (built-in)>, &#39;_thread&#39;: <module &#39;_thread&#39; (built-in)>, &#39;_weakref&#39;: <module &#39;_weakref&#39; (built-in)>, &#39;winreg&#39;: <module &#39;winreg&#39; (built-in)>, &#39;time&#39;: <module &#39;time&#39; (built-in)>, &#39;zipimport&#39;: <module &#39;zipimport&#39; (frozen)>, &#39;_codecs&#39;: <module &#39;_codecs&#39; (built-in)>, &#39;codecs&#39;: <module &#39;codecs&#39; from &#39;D:\\Python3.8\\lib\\codecs.py&#39;>, &#39;encodings.aliases&#39;: <module &#39;encodings.aliases&#39; from &#39;D:\\Python3.8\\lib\\encodings\\aliases.py&#39;>, &#39;encodings&#39;: <module &#39;encodings&#39; from &#39;D:\\Python3.8\\lib\\encodings\\__init__.py&#39;>, &#39;encodings.utf_8&#39;: <module &#39;encodings.utf_8&#39; from &#39;D:\\Python3.8\\lib\\encodings\\utf_8.py&#39;>, &#39;_codecs_cn&#39;: <module &#39;_codecs_cn&#39; (built-in)>, &#39;_multibytecodec&#39;: <module &#39;_multibytecodec&#39; (built-in)>, &#39;encodings.gbk&#39;: <module &#39;encodings.gbk&#39; from &#39;D:\\Python3.8\\lib\\encodings\\gbk.py&#39;>, &#39;_signal&#39;: <module &#39;_signal&#39; (built-in)>, &#39;__main__&#39;: <module &#39;__main__&#39; (built-in)>, &#39;encodings.latin_1&#39;: <module &#39;encodings.latin_1&#39; from &#39;D:\\Python3.8\\lib\\encodings\\latin_1.py&#39;>, &#39;_abc&#39;: <module &#39;_abc&#39; (built-in)>, &#39;abc&#39;: <module &#39;abc&#39; from &#39;D:\\Python3.8\\lib\\abc.py&#39;>, &#39;io&#39;: <module &#39;io&#39; from &#39;D:\\Python3.8\\lib\\io.py&#39;>, &#39;_stat&#39;: <module &#39;_stat&#39; (built-in)>, &#39;stat&#39;: <module &#39;stat&#39; from &#39;D:\\Python3.8\\lib\\stat.py&#39;>, &#39;_collections_abc&#39;: <module &#39;_collections_abc&#39; from &#39;D:\\Python3.8\\lib\\_collections_abc.py&#39;>, &#39;genericpath&#39;: <module &#39;genericpath&#39; from &#39;D:\\Python3.8\\lib\\genericpath.py&#39;>, &#39;ntpath&#39;: <module &#39;ntpath&#39; from &#39;D:\\Python3.8\\lib\\ntpath.py&#39;>, &#39;os.path&#39;: <module &#39;ntpath&#39; from &#39;D:\\Python3.8\\lib\\ntpath.py&#39;>, &#39;os&#39;: <module &#39;os&#39; from &#39;D:\\Python3.8\\lib\\os.py&#39;>, &#39;_sitebuiltins&#39;: <module &#39;_sitebuiltins&#39; from &#39;D:\\Python3.8\\lib\\_sitebuiltins.py&#39;>, &#39;_locale&#39;: <module &#39;_locale&#39; (built-in)>, &#39;_bootlocale&#39;: <module &#39;_bootlocale&#39; from &#39;D:\\Python3.8\\lib\\_bootlocale.py&#39;>, &#39;types&#39;: <module &#39;types&#39; from &#39;D:\\Python3.8\\lib\\types.py&#39;>, &#39;importlib._bootstrap&#39;: <module &#39;importlib._bootstrap&#39; (frozen)>, &#39;importlib._bootstrap_external&#39;: <module &#39;importlib._bootstrap_external&#39; (frozen)>, &#39;warnings&#39;: <module &#39;warnings&#39; from &#39;D:\\Python3.8\\lib\\warnings.py&#39;>, &#39;importlib&#39;: <module &#39;importlib&#39; from &#39;D:\\Python3.8\\lib\\importlib\\__init__.py&#39;>, &#39;importlib.machinery&#39;: <module &#39;importlib.machinery&#39; from &#39;D:\\Python3.8\\lib\\importlib\\machinery.py&#39;>, &#39;importlib.abc&#39;: <module &#39;importlib.abc&#39; from &#39;D:\\Python3.8\\lib\\importlib\\abc.py&#39;>, &#39;_operator&#39;: <module &#39;_operator&#39; (built-in)>, &#39;operator&#39;: <module &#39;operator&#39; from &#39;D:\\Python3.8\\lib\\operator.py&#39;>, &#39;keyword&#39;: <module &#39;keyword&#39; from &#39;D:\\Python3.8\\lib\\keyword.py&#39;>, &#39;_heapq&#39;: <module &#39;_heapq&#39; (built-in)>, &#39;heapq&#39;: <module &#39;heapq&#39; from &#39;D:\\Python3.8\\lib\\heapq.py&#39;>, &#39;itertools&#39;: <module &#39;itertools&#39; (built-in)>, &#39;reprlib&#39;: <module &#39;reprlib&#39; from &#39;D:\\Python3.8\\lib\\reprlib.py&#39;>, &#39;_collections&#39;: <module &#39;_collections&#39; (built-in)>, &#39;collections&#39;: <module &#39;collections&#39; from &#39;D:\\Python3.8\\lib\\collections\\__init__.py&#39;>, &#39;_functools&#39;: <module &#39;_functools&#39; (built-in)>, &#39;functools&#39;: <module &#39;functools&#39; from &#39;D:\\Python3.8\\lib\\functools.py&#39;>, &#39;contextlib&#39;: <module &#39;contextlib&#39; from &#39;D:\\Python3.8\\lib\\contextlib.py&#39;>, &#39;importlib.util&#39;: <module &#39;importlib.util&#39; from &#39;D:\\Python3.8\\lib\\importlib\\util.py&#39;>, &#39;mpl_toolkits&#39;: <module &#39;mpl_toolkits&#39; (namespace)>, &#39;site&#39;: <module &#39;site&#39; from &#39;D:\\Python3.8\\lib\\site.py&#39;>, &#39;atexit&#39;: <module &#39;atexit&#39; (built-in)>}

sys.modules.keys()

返回所有已导入的模块名列表

>>> import sys
>>> sys.modules.keys()
dict_keys([&#39;sys&#39;, &#39;builtins&#39;, &#39;_frozen_importlib&#39;, &#39;_imp&#39;, &#39;_warnings&#39;, &#39;_frozen_importlib_external&#39;, &#39;_io&#39;, &#39;marshal&#39;, &#39;nt&#39;, &#39;_thread&#39;, &#39;_weakref&#39;, &#39;winreg&#39;, &#39;time&#39;, &#39;zipimport&#39;, &#39;_codecs&#39;, &#39;codecs&#39;, &#39;encodings.aliases&#39;, &#39;encodings&#39;, &#39;encodings.utf_8&#39;, &#39;_codecs_cn&#39;, &#39;_multibytecodec&#39;, &#39;encodings.gbk&#39;, &#39;_signal&#39;, &#39;__main__&#39;, &#39;encodings.latin_1&#39;, &#39;_abc&#39;, &#39;abc&#39;, &#39;io&#39;, &#39;_stat&#39;, &#39;stat&#39;, &#39;_collections_abc&#39;, &#39;genericpath&#39;, &#39;ntpath&#39;, &#39;os.path&#39;, &#39;os&#39;, &#39;_sitebuiltins&#39;, &#39;_locale&#39;, &#39;_bootlocale&#39;, &#39;types&#39;, &#39;importlib._bootstrap&#39;, &#39;importlib._bootstrap_external&#39;, &#39;warnings&#39;, &#39;importlib&#39;, &#39;importlib.machinery&#39;, &#39;importlib.abc&#39;, &#39;_operator&#39;, &#39;operator&#39;, &#39;keyword&#39;, &#39;_heapq&#39;, &#39;heapq&#39;, &#39;itertools&#39;, &#39;reprlib&#39;, &#39;_collections&#39;, &#39;collections&#39;, &#39;_functools&#39;, &#39;functools&#39;, &#39;contextlib&#39;, &#39;importlib.util&#39;, &#39;mpl_toolkits&#39;, &#39;site&#39;, &#39;atexit&#39;])

sys.getdefaultencoding()

返回Unicode实现使用的当前默认字符串编码的名称。

>>> import sys
>>> sys.getdefaultencoding()&#39;utf-8&#39;

sys.getfilesystemencoding()

返回用于在Unicode文件名和字节文件名之间进行转换的编码名称

>>> import sys
>>> sys.getfilesystemencoding()utf-8

sys.getrecursionlimit()

返回最大递归次数

>>> import sys
>>> sys.getrecursionlimit()  
# 查看当前解释器的最大递归深度1000

sys.setrecursionlimit(num)

设置最大递归次数

>>> import sys
>>> sys.setrecursionlimit(1100)  
# 将解释器的最大递归深度设置为1100
>>> sys.getrecursionlimit()  
# 再次查看当前解释器的最大递归深度1100

sys.getsizeof()

获取对象占用的内存大小(用字节表示)

>>> import sys
>>> for obj in [int(), float(), list(), tuple(), set(), dict(), object]:...     print(str(obj.__class__).ljust(20), sys.getsizeof(obj))...<class &#39;int&#39;>        24<class &#39;float&#39;>      24<class &#39;list&#39;>       56<class &#39;tuple&#39;>      40<class &#39;set&#39;>        216<class &#39;dict&#39;>       232<class &#39;type&#39;>       416

sys.getrefcount(obj)

返回obj的引用计数。返回的计数通常比预期的高一个,因为它包含(临时)引用作为参数。

>>> import sys
>>> a = [1,2,3]
>>> b = a
>>> c = b
>>> sys.getrefcount(a)4

sys.exc_info()

获取当前正在处理的异常类,exc_type、exc_value、exc_traceback当前处理的异常详细信息

>>> import sys
>>> sys.exc_info()(None, None, None)

sys.getwindowsversion()

获取Windows的版本,Windows系统中有效

>>> import sys
>>> sys.getwindowsversion()sys.getwindowsversion(major=10, minor=0, build=19041, platform=2, service_pack=&#39;&#39;)

sys.stdin.readline()

从标准输入读一行,会读取末尾的换行符

sys.stdout.write()

向标准输出写入内容

>>> import sys
>>> sys.stdout.write("hello world")hello world11

推荐学习:python教程

以上就是Python基础学习之标准库sys总结的详细内容,更多请关注其它相关文章!