我是靠谱客的博主 勤恳花生,最近开发中收集的这篇文章主要介绍Python基础学习之标准库sys总结,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

推荐学习:python学习教程

sys作用

>>> import sys
登录后复制

常用变量

sys.version

>>> 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

>>> import sys
>>> sys.maxsize
9223372036854775807
登录后复制

sys.maxunicode

>>> 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

import sysprint(sys.argv)
登录后复制
E:Python> python 2.py hello python['2.py', 'hello', 'python']
登录后复制

sys.executable

>>> import sys
>>> sys.executable
'D:\Python3.8\python.exe'
登录后复制

sys.byteorder

>>> import sys>>> sys.byteorder'little'
登录后复制

sys.version_info

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0)
登录后复制

sys.api_version

>>> import sys>>> sys.api_version1013
登录后复制

sys.stdin/sys.stdout/sys.stderr

>>> import sys
>>> sys.stdin
<_io.TextIOWrapper name='<stdin>' mode='r' encoding='utf-8'>
>>> sys.stdout
<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
>>> sys.stderr
<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>
登录后复制

常用方法

sys.exit()

import sysprint(sys.exit(0))print(sys.exit(1))
登录后复制

sys.modules

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

sys.modules.keys()

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

sys.getdefaultencoding()

>>> import sys
>>> sys.getdefaultencoding()'utf-8'
登录后复制

sys.getfilesystemencoding()

>>> 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 'int'>        24<class 'float'>      24<class 'list'>       56<class 'tuple'>      40<class 'set'>        216<class 'dict'>       232<class 'type'>       416
登录后复制

sys.getrefcount(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()

>>> import sys
>>> sys.getwindowsversion()sys.getwindowsversion(major=10, minor=0, build=19041, platform=2, service_pack='')
登录后复制

sys.stdin.readline()

sys.stdout.write()

>>> import sys
>>> sys.stdout.write("hello world")hello world11
登录后复制

推荐学习:python教程

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

最后

以上就是勤恳花生为你收集整理的Python基础学习之标准库sys总结的全部内容,希望文章能够帮你解决Python基础学习之标准库sys总结所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部