python学习-sys模块

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

1.sys.argv

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

             

#!/usr/bin/env python
#*-*coding:UTF-8 *-*
import sys

if __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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值