python——sys模块

简介: python——sys模块

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


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


我们先整体看一下 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 sys
if __name__ == '__main__':
    args = sys.argv
    print(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 sys
print(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 sys
s1 = input()
s2 = sys.stdin.readline()
print(s1)
print(s2)

stderr

错误输出。看下示例:

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

exit()

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

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


相关文章
|
6天前
|
JSON API 数据格式
30天拿下Python之requests模块
30天拿下Python之requests模块
18 7
|
5天前
|
人工智能 数据可视化 搜索推荐
Python异常模块与包
Python异常模块与包
|
6天前
|
开发者 Python
30天拿下Python之模块和包
30天拿下Python之模块和包
|
7天前
|
Python
Python中正则表达式(re模块)用法详解
Python中正则表达式(re模块)用法详解
14 2
|
5天前
|
Linux Python Windows
一个Python模块Pendulum的问题
一个Python模块Pendulum的问题
11 0
|
6天前
|
API Python
30天拿下Python之matplotlib模块
30天拿下Python之matplotlib模块
|
6天前
|
SQL 数据处理 数据库
30天拿下Python之pandas模块
30天拿下Python之pandas模块
|
6天前
|
存储 索引 Python
30天拿下Python之numpy模块
30天拿下Python之numpy模块
10 0
|
6天前
|
开发者 Python
30天拿下Python之logging模块
30天拿下Python之logging模块
|
6天前
|
安全 索引 Python
30天拿下Python之collections模块
30天拿下Python之collections模块
下一篇
无影云桌面