使用前,先导入。 >>> import sys sys模块常用命令: sys.argv 命令行参数List,第一个元素是程序本身路径 >>> sys.argv [''] sys.modules 返回系统导入的模块字段,key是模块名,value是模块 {'tokenize': <module 'tokenize' from 'D:\Python27\lib\tokenize.pyc'>, 'heapq': <module 'heapq' from 'D:\Python27\lib\heapq.pyc'>, '__future__': <modul e '__future__' from 'D:\Python27\lib\__future__.pyc'>, 'copy_reg': <module 'copy_reg' from 'D:\Python27\lib\copy_reg.pyc'>, 'sre_compile': <module 'sr e_compile' from 'D:\Python27\lib\sre_compile.pyc'>, '_collections': <module '_collections' (built-in)>, 'tempfile': <module 'tempfile' from 'D:\Python 27\lib\tempfile.pyc'>, 'locale': <module 'locale' from 'D:\Python27\lib\locale.pyc'>, '_sre': <module '_sre' (built-in)>, 'functools': <module 'functo ols' from 'D:\Python27\lib\functools.pyc'>, 'encodings': <module 'encodings' from 'D:\Python27\lib\encodings\__init__.pyc'>, 'site': <module 'site' fr om 'D:\Python27\lib\site.pyc'>, '__builtin__': <module '__builtin__' (built-in)>, 'sysconfig': <module 'sysconfig' from 'D:\Python27\lib\sysconfig.pyc '>, 'operator': <module 'operator' (built-in)>, 'random': <module 'random' from 'D:\Python27\lib\random.pyc'>, '__main__': <module '__main__' (built-i n)>, 'types': <module 'types' from 'D:\Python27\lib\types.pyc'>, 'encodings.encodings': None, 'hashlib': <module 'hashlib' from 'D:\Python27\lib\hashl ib.pyc'>, 'encodings.gbk': <module 'encodings.gbk' from 'D:\Python27\lib\encodings\gbk.pyc'>, '_heapq': <module '_heapq' (built-in)>, 'abc': <module ' abc' from 'D:\Python27\lib\abc.pyc'>, '_bisect': <module '_bisect' (built-in)>, 'strop': <module 'strop' (built-in)>, '_random': <module '_random' (bu ilt-in)>, '_weakrefset': <module '_weakrefset' from 'D:\Python27\lib\_weakrefset.pyc'>, 'encodings._codecs_cn': None, 'errno': <module 'errno' (built- in)>, 'binascii': <module 'binascii' (built-in)>, 'encodings.codecs': None, 'sre_constants': <module 'sre_constants' from 'D:\Python27\lib\sre_constan ts.pyc'>, 're': <module 're' from 'D:\Python27\lib\re.pyc'>, '_abcoll': <module '_abcoll' from 'D:\Python27\lib\_abcoll.pyc'>, 'collections': <module 'collections' from 'D:\Python27\lib\collections.pyc'>, 'ntpath': <module 'ntpath' from 'D:\Python27\lib\ntpath.pyc'>, '_codecs': <module '_codecs' (bu ilt-in)>, 'encodings._multibytecodec': None, 'nt': <module 'nt' (built-in)>, '_warnings': <module '_warnings' (built-in)>, 'math': <module 'math' (bui lt-in)>, 'genericpath': <module 'genericpath' from 'D:\Python27\lib\genericpath.pyc'>, 'stat': <module 'stat' from 'D:\Python27\lib\stat.pyc'>, 'zipim port': <module 'zipimport' (built-in)>, 'encodings.__builtin__': None, 'string': <module 'string' from 'D:\Python27\lib\string.pyc'>, 'warnings': <mod ule 'warnings' from 'D:\Python27\lib\warnings.pyc'>, 'UserDict': <module 'UserDict' from 'D:\Python27\lib\UserDict.pyc'>, 'inspect': <module 'inspect' from 'D:\Python27\lib\inspect.pyc'>, '_multibytecodec': <module '_multibytecodec' (built-in)>, 'itertools': <module 'itertools' (built-in)>, 'repr': <module 'repr' from 'D:\Python27\lib\repr.pyc'>, 'sys': <module 'sys' (built-in)>, '_hashlib': <module '_hashlib' from 'D:\Python27\DLLs\_hashlib.pyd' >, 'imp': <module 'imp' (built-in)>, 'codecs': <module 'codecs' from 'D:\Python27\lib\codecs.pyc'>, 'os.path': <module 'ntpath' from 'D:\Python27\lib\ ntpath.pyc'>, 'pkgutil': <module 'pkgutil' from 'D:\Python27\lib\pkgutil.pyc'>, '_functools': <module '_functools' (built-in)>, '_codecs_cn': <module '_codecs_cn' (built-in)>, '_locale': <module '_locale' (built-in)>, 'thread': <module 'thread' (built-in)>, 'keyword': <module 'keyword' from 'D:\Pyth on27\lib\keyword.pyc'>, 'bisect': <module 'bisect' from 'D:\Python27\lib\bisect.pyc'>, 'signal': <module 'signal' (built-in)>, 'traceback': <module 't raceback' from 'D:\Python27\lib\traceback.pyc'>, 'pydoc': <module 'pydoc' from 'D:\Python27\lib\pydoc.pyc'>, 'linecache': <module 'linecache' from 'D: \Python27\lib\linecache.pyc'>, 'token': <module 'token' from 'D:\Python27\lib\token.pyc'>, 'opcode': <module 'opcode' from 'D:\Python27\lib\opcode.pyc '>, 'encodings.aliases': <module 'encodings.aliases' from 'D:\Python27\lib\encodings\aliases.pyc'>, 'exceptions': <module 'exceptions' (built-in)>, 's re_parse': <module 'sre_parse' from 'D:\Python27\lib\sre_parse.pyc'>, 'cStringIO': <module 'cStringIO' (built-in)>, 'os': <module 'os' from 'D:\Python 27\lib\os.pyc'>, '_weakref': <module '_weakref' (built-in)>, 'dis': <module 'dis' from 'D:\Python27\lib\dis.pyc'>} sys.modules.keys() 返回所有已经导入的模块名 >>> sys.modules.keys() ['tokenize', 'heapq', '__future__', 'copy_reg', 'sre_compile', '_collections', 'tempfile', 'locale', '_sre', 'functools', 'encodings', 'site', '__buil tin__', 'sysconfig', 'operator', 'random', '__main__', 'types', 'encodings.encodings', 'hashlib', 'encodings.gbk', '_heapq', 'abc', '_bisect', 'strop' , '_random', '_weakrefset', 'encodings._codecs_cn', 'errno', 'binascii', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'collections', 'ntpath' , '_codecs', 'encodings._multibytecodec', 'nt', '_warnings', 'math', 'genericpath', 'stat', 'zipimport', 'encodings.__builtin__', 'string', 'warnings' , 'UserDict', 'inspect', '_multibytecodec', 'itertools', 'repr', 'sys', '_hashlib', 'imp', 'codecs', 'os.path', 'pkgutil', '_functools', '_codecs_cn', '_locale', 'thread', 'keyword', 'bisect', 'signal', 'traceback', 'pydoc', 'linecache', 'token', 'opcode', 'encodings.aliases', 'exceptions', 'sre_par se', 'cStringIO', 'os', '_weakref', 'dis'] sys.modules.values() 返回所有已经导入的模块 >>> sys.modules.values() [<module 'tokenize' from 'D:\Python27\lib\tokenize.pyc'>, <module 'heapq' from 'D:\Python27\lib\heapq.pyc'>, <module '__future__' from 'D:\Python27\li b\__future__.pyc'>, <module 'copy_reg' from 'D:\Python27\lib\copy_reg.pyc'>, <module 'sre_compile' from 'D:\Python27\lib\sre_compile.pyc'>, <module '_ collections' (built-in)>, <module 'tempfile' from 'D:\Python27\lib\tempfile.pyc'>, <module 'locale' from 'D:\Python27\lib\locale.pyc'>, <module '_sre' (built-in)>, <module 'functools' from 'D:\Python27\lib\functools.pyc'>, <module 'encodings' from 'D:\Python27\lib\encodings\__init__.pyc'>, <module ' site' from 'D:\Python27\lib\site.pyc'>, <module '__builtin__' (built-in)>, <module 'sysconfig' from 'D:\Python27\lib\sysconfig.pyc'>, <module 'operato r' (built-in)>, <module 'random' from 'D:\Python27\lib\random.pyc'>, <module '__main__' (built-in)>, <module 'types' from 'D:\Python27\lib\types.pyc'> , None, <module 'hashlib' from 'D:\Python27\lib\hashlib.pyc'>, <module 'encodings.gbk' from 'D:\Python27\lib\encodings\gbk.pyc'>, <module '_heapq' (bu ilt-in)>, <module 'abc' from 'D:\Python27\lib\abc.pyc'>, <module '_bisect' (built-in)>, <module 'strop' (built-in)>, <module '_random' (built-in)>, <m odule '_weakrefset' from 'D:\Python27\lib\_weakrefset.pyc'>, None, <module 'errno' (built-in)>, <module 'binascii' (built-in)>, None, <module 'sre_con stants' from 'D:\Python27\lib\sre_constants.pyc'>, <module 're' from 'D:\Python27\lib\re.pyc'>, <module '_abcoll' from 'D:\Python27\lib\_abcoll.pyc'>, <module 'collections' from 'D:\Python27\lib\collections.pyc'>, <module 'ntpath' from 'D:\Python27\lib\ntpath.pyc'>, <module '_codecs' (built-in)>, No ne, <module 'nt' (built-in)>, <module '_warnings' (built-in)>, <module 'math' (built-in)>, <module 'genericpath' from 'D:\Python27\lib\genericpath.pyc '>, <module 'stat' from 'D:\Python27\lib\stat.pyc'>, <module 'zipimport' (built-in)>, None, <module 'string' from 'D:\Python27\lib\string.pyc'>, <modu le 'warnings' from 'D:\Python27\lib\warnings.pyc'>, <module 'UserDict' from 'D:\Python27\lib\UserDict.pyc'>, <module 'inspect' from 'D:\Python27\lib\i nspect.pyc'>, <module '_multibytecodec' (built-in)>, <module 'itertools' (built-in)>, <module 'repr' from 'D:\Python27\lib\repr.pyc'>, <module 'sys' ( built-in)>, <module '_hashlib' from 'D:\Python27\DLLs\_hashlib.pyd'>, <module 'imp' (built-in)>, <module 'codecs' from 'D:\Python27\lib\codecs.pyc'>, <module 'ntpath' from 'D:\Python27\lib\ntpath.pyc'>, <module 'pkgutil' from 'D:\Python27\lib\pkgutil.pyc'>, <module '_functools' (built-in)>, <module '_codecs_cn' (built-in)>, <module '_locale' (built-in)>, <module 'thread' (built-in)>, <module 'keyword' from 'D:\Python27\lib\keyword.pyc'>, <module 'bisect' from 'D:\Python27\lib\bisect.pyc'>, <module 'signal' (built-in)>, <module 'traceback' from 'D:\Python27\lib\traceback.pyc'>, <module 'pydoc' from 'D:\Python27\lib\pydoc.pyc'>, <module 'linecache' from 'D:\Python27\lib\linecache.pyc'>, <module 'token' from 'D:\Python27\lib\token.pyc'>, <modu le 'opcode' from 'D:\Python27\lib\opcode.pyc'>, <module 'encodings.aliases' from 'D:\Python27\lib\encodings\aliases.pyc'>, <module 'exceptions' (built -in)>, <module 'sre_parse' from 'D:\Python27\lib\sre_parse.pyc'>, <module 'cStringIO' (built-in)>, <module 'os' from 'D:\Python27\lib\os.pyc'>, <modul e '_weakref' (built-in)>, <module 'dis' from 'D:\Python27\lib\dis.pyc'>] sys.exc_info() 获取当前正在处理的异常类,exc_type、exc_value、exc_traceback当前处理的异常详细信息 >>> sys.exc_info() (None, None, None) sys.exit(n) 退出程序,正常退出时exit(0) >>> sys.exit(1) C:\Users\Biao> sys.hexversion 获取Python解释程序的版本值,16进制格式如:0x020403F0 >>> sys.hexversion 34014192 sys.version 获取Python解释程序的版本信息 >>> sys.version '2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)]' sys.maxint 最大的Int值 >>> sys.maxint 2147483647 sys.maxunicode 最大的Unicode值 >>> sys.maxunicode 65535 sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 >>> sys.path ['', 'D:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg', 'D:\\Python27\\lib\\site-packages\\django-1.4.2-py2.7.egg', 'C:\\Windows\\system 32\\python27.zip', 'D:\\Python27\\DLLs', 'D:\\Python27\\lib', 'D:\\Python27\\lib\\plat-win', 'D:\\Python27\\lib\\lib-tk', 'D:\\Python27', 'D:\\Python2 7\\lib\\site-packages'] sys.platform 返回操作系统平台名称 >>> sys.platform 'win32' sys.stdout 标准输出 >>> sys.stdout.write('this is Python') this is Python>>> sys.stdin 标准输入 >>> sys.stdin.readline() this is Python 'this is Python\n' sys.stderr 错误输出 >>> sys.stderr.write("this is error") this is error>>> sys.exc_clear() 用来清除当前线程所出现的当前的或最近的错误信息 sys.exec_prefix 返回平台独立的python文件安装的位置 >>> sys.exec_prefix 'D:\\Python27' sys.byteorder 本地字节规则的指示器,big-endian平台的值是’big’,little-endian平台的值是’little’ >>> sys.byteorder 'little' sys.copyright 记录python版权相关的东西 >>> sys.copyright 'Copyright (c) 2001-2012 Python Software Foundation.\nAll Rights Reserved.\n\nCopyright (c) 2000 BeOpen.com.\nAll Rights Reserved.\n\nCopyright (c) 19 95-2001 Corporation for National Research Initiatives.\nAll Rights Reserved.\n\nCopyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\nAl l Rights Reserved.' sys.api_version 解释器的C的API版本 >>> sys.api_version 1013 sys.version_info >>> sys.version_info sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0) ‘final’表示最终,也有’candidate’表示候选,serial表示版本级别,是否有后继的发行 sys.displayhook(value) 如果value非空,这个函数会把他输出到sys.stdout,并且将他保存进__builtin__._.指在python的交互式解释器里,’_’ 代表上次你输入得到的结果,hook是钩子的意思,将上次的结果钩过来 >>> sys.displayhook("this is sys.displayhook()") 'this is sys.displayhook()' sys.getdefaultencoding() 返回当前你所用的默认的字符编码格式 >>> sys.getdefaultencoding() 'ascii' sys.getfilesystemencoding() 返回将Unicode文件名转换成系统文件名的编码的名字 >>> sys.getfilesystemencoding() 'mbcs' sys.setdefaultencoding(name)用来设置当前默认的字符编码,如果name和任何一个可用的编码都不匹配,抛出 LookupError,这个函数只会被site模块的sitecustomize使用,一旦别site模块使用了,他会从sys模块移除 sys.builtin_module_names Python解释器导入的模块列表 >>> sys.builtin_module_names ('__builtin__', '__main__', '_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_c ollections', '_csv', '_functools', '_heapq', '_hotshot', '_io', '_json', '_locale', '_lsprof', '_md5', '_multibytecodec', '_random', '_sha', '_sha256' , '_sha512', '_sre', '_struct', '_subprocess', '_symtable', '_warnings', '_weakref', '_winreg', 'array', 'audioop', 'binascii', 'cPickle', 'cStringIO' , 'cmath', 'datetime', 'errno', 'exceptions', 'future_builtins', 'gc', 'imageop', 'imp', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'oper ator', 'parser', 'signal', 'strop', 'sys', 'thread', 'time', 'xxsubtype', 'zipimport', 'zlib') sys.executable Python解释程序路径 >>> sys.executable 'D:\\Python27\\python.exe' sys.getwindowsversion() 获取Windows的版本 >>> sys.getwindowsversion() sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') 对于这些命令,无需过多的死机硬背,用得多了,自然就记住了。实在记不住,可以使用dir及help命令来查看帮助。 >>> dir(sys) ['__displayhook__', '__doc__', '__egginsert', '__excepthook__', '__name__', '__package__', '__plen', '__stderr__', '__stdin__', '__stdout__', '_clear_ type_cache', '_current_frames', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'c opyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getr efcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'last_traceback', 'last_type', 'last_value', 'long_info', 'maxint', 'maxsize', ' maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'py3kwarning', 'setcheckinterval ', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver'] >>> help(sys.getwindowsversion) Help on built-in function getwindowsversion in module sys: getwindowsversion(...) getwindowsversion() Return information about the running version of Windows as a named tuple. The members are named: major, minor, build, platform, service_pack, service_pack_major, service_pack_minor, suite_mask, and product_type. For backward compatibility, only the first 5 items are available by indexing. All elements are numbers, except service_pack which is a string. Platform may be 0 for win32s, 1 for Windows 9x/ME, 2 for Windows NT/2000/XP/Vista/7, 3 for Windows CE. Product_type may be 1 for a workstation, 2 for a domain controller, 3 for a server. 转载请保留固定链接: https://linuxeye.com/program/1769.html |