Python基础(二十):sys模块
创始人
2025-05-31 13:56:38
0
  1. 简介

sys 模块主要负责与 Python 解释器进行交互,该模块提供了一系列用于控制 Python 运行环境的函数和变量。

之前我们说过 os 模块,该模块与 sys 模块从名称上看着好像有点类似,实际上它们之间是没有什么关系的,os 模块主要负责与操作系统进行交互。

  1. 使用

我们先整体看一下 sys 模块都包含哪些内容,如下所示:

>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_getframe', '_git', '_home', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'set_asyncgen_hooks', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']

对于一些相对常用的变量和函数,我们下面再来具体看一下。

argv:

返回传递给 Python 脚本的命令行参数列表。看下示例:

import sysif __name__ == '__main__':args = sys.argvprint(args)print(args[1])

上面文件名为:test.py,我们在控制台使用命令:python test.py 123 abc 执行一下,执行结果如下:

['test.py', '123', 'abc']
123

version:

返回 Python 解释器的版本信息。

winver:

返回 Python 解释器主版号。

platform:

返回操作系统平台名称。

path:

返回模块的搜索路径列表。

maxsize:

返回支持的最大整数值。

maxunicode:

返回支持的最大 Unicode 值。

copyright:

返回 Python 版权信息。

modules:

以字典类型返回系统导入的模块。

byteorder:

返回本地字节规则的指示器。

executable:

返回 Python 解释器所在路径。

import sysprint(sys.version)
print(sys.winver)
print(sys.platform)
print(sys.path)
print(sys.maxsize)
print(sys.maxunicode)
print(sys.copyright)
print(sys.modules)
print(sys.byteorder)
print(sys.executable)

stdout:

标准输出。看下示例:

import sys# 下面两行代码等价
sys.stdout.write('Hi' + '\n')
print('Hi')

stdin:

标准输入。看下示例:

import syss1 = input()
s2 = sys.stdin.readline()
print(s1)
print(s2)

stderr:

错误输出。看下示例:

import syssys.stderr.write('this is a error message')

exit():

退出当前程序。看下示例:

import sysprint('Hi')
sys.exit()
print('Jhon')

getdefaultencoding():

返回当前默认字符串编码的名称。

getrefcount(obj):

返回对象的引用计数。

getrecursionlimit():

返回支持的递归深度。

getsizeof(object[, default]):

以字节为单位返回对象的大小。

setswitchinterval(interval):

设置线程切换的时间间隔。

getswitchinterval():

返回线程切换时间间隔。

import sysprint(sys.getdefaultencoding())
print(sys.getrefcount('123456'))
print(sys.getrecursionlimit())
print(sys.getsizeof('abcde'))
sys.setswitchinterval(1)
print(sys.getswitchinterval())

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...