Python的系统模块包括:sys, os, glob, socket, threading, _thread, queue, time, timeit, subprocess, multiprocessing, signal, select, shutil, tempfile等。其中大多数系统级接口集中在:sys和os两个模块。
sys模块包含:
- 平台与版本的信息,如sys.platform, sys.maxsize, sys.version
- 模块搜索路径sys.path
- 模块表sys.modules,这是一个包含python程序中import进来的模块的name:module信息的字典
- 异常信息,如sys.exc_info()
- 命令行参数sys.argv
- 标准流,如sys.stdin, sys.stdout, sys.stderr
- 程序退出调用sys.exit
sys模块主要是提供用来访问某些对象,这些对象包括被使用过或者被那些有着强大的互动解释能力的翻译器和方法维护过的对象。
sys.argv
#!/usr/bin/python # -*- coding: utf-8 -*- from sys import argv script, first, second, third = argv print "The script is called:", script print "Your first variable is:", first print "Your second variable is:", second print "Your third variable is:", third
运行结果
$ python ex13.py first 2nd 3rd The script is called: ex13.py Your first variable is: first Your second variable is: 2nd Your third variable is: 3rd
sys.argv
变量是一个字符串的 列表
Sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径
sys.argv[]的官方解释
- sys.argv
-
The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string.
To loop over the standard input, or the list of files given on the command line, see the fileinput module.
sys.path
path 列表是一个由目录名构成的列表, Python 从中查找扩展模块( Python 源 模块, 编译模块,或者二进制扩展). 启动 Python 时,这个列表从根据内建规则, PYTHONPATH 环境变量的内容, 以及注册表( Windows 系统)等进行初始化. 由 于它只是一个普通的列表, 你可以在程序中对它进行操作
path[0]是当前目录
#!/usr/bin/python # -*- coding: utf-8 -*- import sys for l in sys.path: print l
运行结果
/Users/goodspeed/Documents/Code/python/learnPythonHW/mystuff /Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg /usr/local/lib/wxPython-2.9.3.1/lib/python2.7/site-packages /usr/local/lib/wxPython-2.9.3.1/lib/python2.7/site-packages/wx-2.9.3-osx_cocoa /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC /Library/Python/2.7/site-packages /usr/local/lib/wxPython-2.9.3.1/lib/python2.7
sys.builtin_module_names
- sys.builtin_module_names
-
A tuple of strings giving the names of all modules that are compiled into this Python interpreter. (This information is not available in any other way — modules.keys() only lists the imported modules.)
内建模块 是一个元组 包含内容为
('__builtin__', '__main__', '_ast', '_codecs', '_sre', '_symtable', '_warnings', '_weakref', 'errno', 'exceptions', 'gc', 'imp', 'marshal', 'posix', 'pwd', 'signal', 'sys', 'thread', 'xxsubtype', 'zipimport')
sys 模块查找已导入的模块为
import sys print sys.modules.keys() ['os.path', 'os', 'exceptions', '_ _main_ _', 'ntpath', 'strop', 'nt', 'sys', '_ _builtin_ _', 'site', 'signal', 'UserDict', 'string', 'stat']
未完待续....
sys 参考文档 http://docs.python.org/library/sys.html