python学习-sys模块

来源:互联网 发布:php 命名空间的用法 编辑:程序博客网 时间:2024/05/16 19:39

只写比较常用的,sys模块的其他DATA请参考help(sys).

1.sys.argv

   shell脚本通过$x(x为数字)获取被送入脚本的参数,而python通过sys.argv做这一点,代码如下:

             

#!/usr/bin/env python#*-*coding:UTF-8 *-*import sysif __name__=='__main__':        print sys.argv

测试:

john@john-pad:~/python_workspace/core$ python  sys_demo.py var1 var2['sys_demo.py', 'var1', 'var2']

argv是一个列表,第一项即argv[0]是脚本的名称,并依次存储送进来的参数。


2.sys.exit()

  该函数用以退出python解释器,接受一个参数,以下为该函数的doc string:

    Exit the interpreter by raising SystemExit(status).
    If the status is omitted or None, it defaults to zero (i.e., success).
    If the status is numeric, it will be used as the system exit status.
    If it is another kind of object, it will be printed and the system
    exit status will be one (i.e., failure).

    

3.sys.path

   是一个包含了字符串的列表,当需要加载某个module(模块)时就从这个列表中的字符串所代表的系统路径进行查找,可以试着打印一下该字段。

  因为这是一个列表(list),所以可以通过列表的操作添加或者修改module的搜索路径,比如,我自己新写了一个module,需要将该module所在的目录添加到sys.path列表中

  

4.sys.modules

  是一个包含了已被加载模块的列表,


5.sys.stdin sys.stdout sys.stderr

   分别对应标准输入流 标准输出流 标准错误流

0 0
原创粉丝点击