Python3中常用模块-sys模块

目录

1:获取模块文档

1.1:使用sys.__doc__查看方法说明

1.2:使用dir函数获取模块中的所有属性和方法

1.3:使用help函数查看某个函数

2:模块常用方法

3:解析命令行参数


sys模块和os模块是Python系统相关工具集的核心部分,其主要处理系统相关的功能,下面首先来介绍下sys模块。

1:获取模块文档

1.1:使用sys.__doc__查看方法说明

print(sys.__doc__)

"""
This module provides access to some objects used or maintained by the
interpreter and to functions that interact strongly with the interpreter.

Dynamic objects:

argv -- command line arguments; argv[0] is the script pathname if known
path -- module search path; path[0] is the script directory, else ''
modules -- dictionary of loaded modules

displayhook -- called to show results in an interactive session
excepthook -- called to handle any uncaught exception other than SystemExit
  To customize printing in an interactive session or to install a custom
  top-level exception handler, assign other functions to replace these.

stdin -- standard input file object; used by input()
stdout -- standard output file object; used by print()
.................

"""

1.2:使用dir函数获取模块中的所有属性和方法

print(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']

"""

或者一行一行打印出来

for item in dir(sys):
	print(item )


"""

__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
..........
后面还有......

"""

1.3:使用help函数查看某个函数

print(help(sys.exc_info)) 

"""
Help on built-in function exc_info in module sys:

exc_info(...)
    exc_info() -> (type, value, traceback)
    
    Return information about the most recent exception caught by an except
    clause in the current stack frame or in an older stack frame.
"""

2:模块常用方法

函数    说明
sys.argv接收命令行的参数,是一个列表,第一个元素为文件名本身
sys. path 模块搜索路径
sys.modules当前进程加载的模块
sys.exc_info()最近抛出的Python异常信息
sys.stdin 标准输入流
sys.stdout标准输出流
sys.stderr 标准错误流
sys.platformpython解释程序的版本信息
sys.platform 操作系统平台名称
sys.exit(n) 若 n 为0,则正常退出;其他都是异常退出,可以捕获
os.abort()  强制退出

3:解析命令行参数

import sys

def getopts(argv):
	opts = {}
	while argv:
		if argv[0][0] == '-':
			opts[argv[0]] = argv[1]
			argv = argv[2:]
		else:
			argv = argv[1:]
	
	return opts

if __name__ == '__main__':
	print(sys.argv)
	margs  = getopts(sys.argv)
	print(margs)

结果:

 

 

 

 


 
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值