Python sys模块怎么使用

  

Python sys模块怎么使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

sys模块代表了Python解释器,主要用于获取和Python解释器相关的信息。

在Python的交互式解释器中先导入sys模块,然后输入(e, e dir (sys)如果不是e.startswith(& # 39; _ # 39;)]命令(sys模块没有__all__变量),可以看到如下输出结果:

在祝辞祝辞,[e  for  e 拷贝dir(系统),if  not  e.startswith (& # 39; _ # 39;))   [& # 39;api_version& # 39;,, & # 39; argv # 39;,, & # 39; base_exec_prefix& # 39;,, & # 39; base_prefix& # 39;,, & # 39; builtin_module_names& # 39;,, & # 39; byteorder& # 39;,, & # 39; call_tracing& # 39;,,   & # 39;callstats& # 39;,, & # 39;版权# 39;,,& # 39;displayhook& # 39;,, & # 39; dllhandle& # 39;,, & # 39; dont_write_bytecode& # 39;,, & # 39; exc_info& # 39;,, & # 39; excepthook& # 39;,, & # 39; exec_prefix& # 39;   ,& # 39;可执行# 39;,,& # 39;退出# 39;,,& # 39;旗帜# 39;,,& # 39;float_info& # 39;,, & # 39; float_repr_style& # 39;,, & # 39; get_asyncgen_hooks& # 39;,, & # 39; get_coroutine_wrapper& # 39;,,   ,& # 39;getallocatedblocks& # 39;,, & # 39; getcheckinterval& # 39;,, & # 39; getdefaultencoding& # 39;,, & # 39; getfilesystemencodeerrors& # 39;,, & # 39; getfilesystemencoding& # 39;,,   ,& # 39;getprofile& # 39;,, & # 39; getrecursionlimit& # 39;,, & # 39; getrefcount& # 39;,, & # 39; getsizeof& # 39;,, & # 39; getswitchinterval& # 39;,, & # 39; gettrace& # 39;,, & # 39; getwindowsversion& # 39;   & # 39;才能hash_info& # 39;,, & # 39; hexversion& # 39;,, & # 39;实现# 39;,,& # 39;int_info& # 39;,, & # 39;实习生# 39;,,& # 39;is_finalizing& # 39;,, & # 39; last_traceback& # 39;,, & # 39; last_type& # 39;,,   & # 39;才能last_value& # 39;,, & # 39;最大尺寸# 39;,,& # 39;maxunicode& # 39;,, & # 39; meta_path& # 39;,, & # 39;模块# 39;,,& # 39;path & # 39;,, & # 39; path_hooks& # 39;,, & # 39; path_importer_cache& # 39;,,   & # 39;才能平台# 39;,,& # 39;前缀# 39;,,& # 39;set_asyncgen_hooks& # 39;,, & # 39; set_coroutine_wrapper& # 39;,, & # 39; setcheckinterval& # 39;,, & # 39; setprofile& # 39;,,   & # 39;才能setrecursionlimit& # 39;,, & # 39; setswitchinterval& # 39;,, & # 39; settrace& # 39;,, & # 39; stderr # 39;,, & # 39; stdin # 39;,, & # 39; stdout # 39;,, & # 39; thread_info& # 39;,, & # 39;版本# 39;   ,,& # 39;version_info& # 39;,, & # 39; warnoptions& # 39;,, & # 39; winver& # 39;]

上面列出的程序单元就是sys模块所包含的全部程序单元(包括变量,函数等),读者不要被它们吓着了,以为这些全都需要记下来。实际上完全没有必要,通常都是用到哪些模块就去查阅其对应的说明文档和参考手册。

祝辞祝辞祝辞[e for e in dir(sys) if not e.startswith('_')]

['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', 'last_traceback', 'last_type', 'last_value', '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']需要说明的是,大部分时候用不到 sys 模块里很冷僻的功能,因此本节只介绍 sys 模块中常用的属性和函数:

sys.argv:获取运行Python程序的命令行参数。其中系统。argv[0]通常就是指该Python程序,系统。argv[1]代表为Python程序提供的第一个参数,系统。argv[2]代表为Python程序提供的第二个参数……依此类推。

系统。byteorder:显示本地字节序的指示符。如果本地字节序是大端模式,则该属性返回大,否则返回。

系统。版权:该属性返回与Python解释器有关的版权信息。

系统。Python可执行:该属性返回解释器在磁盘上的存储路径。

sys.exit():通过引发SystemExit异常来退出程序。将其放在试块中不能阻止最后块的执行。

Python sys模块怎么使用