简明Python教程5
2012-10-06 00:56
323 查看
class ShortInputException(Exception): '''A user-defined exception class.''' def __init__(self, length, atleast): Exception.__init__(self) self.length = length self.atleast = atleast try: s = raw_input('Enter something -->') if len(s) < 3: raise ShortInputException(len(s), 3) #Other work can continue as usual here except EOFError: print '\nWhy did you do an EOF on me?' except ShortInputException, x: print 'ShortInputException: The input was of length %d, \ was expecting at least %d' % (x.length, x.atleast) else: print 'No exception was raised.'
import time try: f = file('poem.txt') while True: line = f.readline() if len(line) == 0: break time.sleep(2) print line, finally: f.close() print 'Cleaning up...closed the file'
相关文章推荐
- 简明python教程 第7章 函数
- 简明python教程:第2章 安装Python
- 简明Python3教程 4.安装
- 简明Python3教程 3.介绍
- 简明Python3教程 8.控制流
- 简明python教程 --C++程序员的视角(六):输入输出
- 简明python教程学习笔记之九-对象参考
- 简明python学习教程五--理解多线程编程
- 支持向量机简明教程及其在python和R下的调参
- 简明python教程 --C++程序员的视角(九):函数式编程、特殊类方法、测试及其他
- 简明Python3教程 14.输入输出
- python进阶之PIL 简明教程 - 基本用法
- 简明Python3教程 首页
- 简明Python3教程 15.异常
- Python 简明教程
- 很有用的一本书,简明 Python 教程
- mongoDB简明教程-python(转)
- 简明Python3教程 3.介绍
- 简明Python3教程 4.安装
- 简明Python3教程 7.运算符和表达式