三十九、Python内置模块之sys


三十九、Python内置模块之sys

文章插图
Python内置模块sys
sys模块代表了Python解释器相关有的信息,主要用来获取解释器的信息 。下面的方法提供查看sys模块下的全部程序单元(包括变量和函数等):
>>> import sys>>> [elem for elem in dir(sys) if not elem.startswith('_')]['abiflags', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix','breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing','callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info','excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info','float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth','get_coroutine_wrApper', 'getallocatedblocks', 'getcheckinterval','getdefaultencoding', 'getdlopenflags', 'getfilesystemencodeerrors','getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount','getsizeof', 'getswitchinterval', 'gettrace', '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', 'prefix', 'ps1', 'ps2','real_prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth','set_coroutine_wrapper', 'setcheckinterval', 'setdlopenflags', 'setprofile','setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin','stdout', 'thread_info', 'version', 'version_info', 'warnoptions']可以看出,sys模块提供了大量的属性和函数,由于有一些功能方法在实际程序开发中用的并不多,下面仅介绍常用的属性和函数 。
  • sys.argv: 获取运行Python程序的命令行参数,是一个列表,第一个参数指运行的程序本身,每二个参数是命令行参数的第一个参数,依次类推...

三十九、Python内置模块之sys

文章插图
命令行参数与argv列表元素的对应关系
【三十九、Python内置模块之sys】# 新建文件argv_test.py, 代码内容如下import sys# 输入argv列表的长度print(f'argv列表的长度为:{len(sys.argv)}')# 打印argv的元素for arg in sys.argv:print(arg)# 使用下面命令执行该文件$ python argv_test.py fengqinyang duguqiubai dongfangbubai renwoxing# 输出argv列表的长度为:5argv_test.pyfengqinyangduguqiubaidongfangbubairenwoxing
  • sys.copyright: Python的解释器的版权信息
>>> print(sys.copyright)Copyright (c) 2001-2019 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.
  • sys.executable: Python解释器在磁盘上的存储路径
>>> print(sys.executable)/Users/david.tian/.virtualenvs/pysparkvenv/bin/python
  • sys.getfilesystemencodeing():当前系统中python文件的字符集
>>> print(sys.getfilesystemencoding())utf-8
  • sys.getrefcount(object): 返回对象的引用计数,当object对象计数个数为0时,系统会回收该对象
>>> print(sys.getrefcount(sys))57>>> myfullname = 'davidekaka'>>> print(sys.getrefcount(myfullname))2
  • sys.getrecursionlimit(): 返回Python解释器支持的递归深度,该属性可以通过setrecursionlimit()来重新设置
>>> print(sys.getrecursionlimit())1000>>> sys.setrecursionlimit(1100)>>> print(sys.getrecursionlimit())1100>>> sys.setrecursionlimit(1000)
  • sys.maxsize: 该属性指Python支持整数的最大值,和系统平台有关系统(32位和64位)不同
>>> print(sys.maxsize)9223372036854775807
  • sys.version: 该属性返回Python解释器的版本
>>> print(sys.version)3.7.4 (v3.7.4:e09359112e, Jul8 2019, 14:54:52) [Clang 6.0 (clang-600.0.57)]
  • sys.platform: 该属性返回Python解释器所在的平台
>>> print(sys.platform)darwin