Python sys 使用说明
获取linux下python的路径,以及执行Python时使用的参数。
代码如下:
- #!/usr/bin/python
- # -*- encoding:utf-8 -*-
- # time:2012-07-06
- import sys;
- print("命令行参数是");
- for i in sys.argv:
- print(i);
- print ('\n\nPython Path is :',sys.path,'\n');
执行的结果如下:
- desktop@desktop-desktop:~/shell_learn/python$ python mytest.py a b c d e f
- 命令行参数是
- mytest.py
- a
- b
- c
- d
- e
- f
- ('\n\nPython Path is :', ['/home/desktop/shell_learn/python', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/pymodules/python2.7/libubuntuone', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol'], '\n')
假如我们需要知道sys这个模块的用法是,我们强烈推荐查询系统自带的帮助,在执行帮助的时候我们也许会碰到诸如:
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- NameError: name 'sys' is not defined
- 类似的错误,解决的办法是在你需要查询的模块是先用import 导入系统模块
- 如:需要查询sys的用法:
- >>> import sys
- >>> help (sys)
- Help on built-in module sys:
-
- NAME
- sys
-
- FILE
- (built-in)
-
- MODULE DOCS
- Http://docs.python.org/library/sys
-
- DESCRIPTioN
- 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 argume
相关文章