第一次碰到try-except(core python programming 2nd Edition 3.6)
# coding: utf-8 # 使用Windows系统,首行'#!/usr/bin/env Pyton'无用,全部改为'# coding: utf-8' 'readtextfile.py -- read and display text file' # get filename
fname = raw_input('Enter filename: ')
print # attempt to open file for reading
try:
fobj = open(fname, 'r') # 尝试打开fname
except IOError, e: # 捕获到的IOError错误的详细原因会被放置在对象e中,然后运行该异常的except代码块(也可以写成'except IOError as e:')
print "*** file open error:", e # 打印异常信息
else: # 如果try没有错误,执行else语句
# display contents to the screen
for eachLine in fobj:
print eachLine, # 逗号是为了抵制print语句自动生成的行结束符(fname里面本身已有行结束符,如果不加逗号,打印出来的内容之间会多空一行)
fobj.close()
与Python异常相关的关键字:
关键字 关键字说明
raise 抛出/引发异常
try/except 捕获异常并处理
pass 忽略异常
as 定义异常实例(except IOError as e)
finally 无论是否出现异常,都执行的代码
else 如果try中的语句没有引发异常,则执行else中的语句
第一次碰到try-except(core python programming 2nd Edition 3.6)的更多相关文章
- 数值运算内建函数(core python programming 2nd edition 5.6.2)
数值运算内建函数 函数 功能 abs(num) 返回 num 的绝对值 coerce(num1, num2) 将num1和num2转换为同一类型,然后以一个元组的形式返回. divmod(num1, ...
- Core Python Programming一书中关于深浅拷贝的错误
该书关于深浅拷贝的论述: 6.20. *Copying Python Objects and Shallow and Deep Copies "when shallow copies are ...
- [core python programming]chapter 7 programming MS office
excel.pyw会有问题,解决如下: 因为python3x中没有tkMessageBox模块,Tkinter改成了tkinter你可以查看你的py当前支持的模块.在交互式命令行下输入>> ...
- 开始 python programming第三版案例分析
最近研究python,打算将python programming第三版案例分析下 但是全书1600多页 比较费时 而且 介绍太多 感觉没有必要! python programming 堪称经典之作 第 ...
- Entity Framework 6 Recipes 2nd Edition 译 -> 目录 -持续更新
因为看了<Entity Framework 6 Recipes 2nd Edition>这本书前面8章的翻译,感谢china_fucan. 从第九章开始,我是边看边译的,没有通读,加之英语 ...
- 第十四周翻译-《Pro SQL Server Internals, 2nd edition》
<Pro SQL Server Internals, 2nd edition> 作者:Dmitri Korotkevitch 翻译:赖慧芳 译文: 设计和优化索引 定义一种应用于所有地方的 ...
- Learning ROS for Robotics Programming - Second Edition(《学习ROS机器人编程-第二版》)
Learning ROS for Robotics Programming - Second Edition <学习ROS机器人编程-第二版> ----Your one-stop guid ...
- Learning ROS for Robotics Programming Second Edition学习笔记(十) indigo Gazebo rviz slam navigation
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 moveit是书的最后一章,由于对机械臂完全不知,看不懂 ...
- Learning ROS forRobotics Programming Second Edition学习笔记(八)indigo rviz gazebo
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...
随机推荐
- Activiti工作流学习-----基于5.19.0版本(6)
七. BPMN的简介 读者了解到这里,应付一般的工作流开发已经足够了.此处应该有华丽的分割线,在工作流项目中核心开发人员主要是对工作流业务设计以及实现,而初级开发人员是对业务功能的代码实现.以后将主要 ...
- 基于Http原理实现Android的图片上传和表单提交
版权声明:本文由张坤 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/794875001483009140 来源:腾云阁 ...
- 《VIM-Adventures攻略》 LEVEL 1-3
此文已转至http://cn.abnerchou.me/2014/03/04/e40e2146/ 上期有人提到此游戏烂尾.其实没有啦,作者是位"贪财"的主,不付费不给玩剩下的章节. ...
- 自定义高级QFileDialog文件过滤器
QFileDialog提供了一个函数---setproxyModel..就是用这个来玩了.就是override filterAcceptsRow的虚函数,里面定制自己的过滤需求.返回bool 下面 ...
- cf492B Vanya and Lanterns
B. Vanya and Lanterns time limit per test 1 second memory limit per test 256 megabytes input standar ...
- hdu5024-Wang Xifeng's Little Plot
此题一开始用暴力做,后来发现斜着走的时候其实暴力不太好写,于是改用搜索写了 #include <iostream> #include <stdio.h> #include &l ...
- Appium 一个测试套件多次启动android应用
AppiumDriver<WebElement> driver; File classpathRoot = new File(System.getProperty("user.d ...
- pyqt例子搜索文本
#!/usr/bin/env python #-*- coding:utf-8 -*- import sip sip.setapi('QString', 2) sip.setapi('QVariant ...
- inline-block及解决空白间距
參考:http://www.jb51.net/css/76707.html http://www.webhek.com/remove-whitespace-inline-block/ inline-b ...
- Android开发学习之Intent具体解释
Intent简单介绍和具体解释: Intent:协助应用间的交互与通信,Intent负责相应用中一次操作的动作.动作涉及的数据.附加数据进行描写叙述. ...