第一次碰到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 ...
随机推荐
- 【object-c基础】Object-c基础之三:面对对象开发@interface,@implementation
1.@interface 在java等语言编程中,创建类都是用class,但在object-c中,用@interface. 例子: @interface circle :NSObject //定 ...
- Java学习笔记--NIO
参考资料:http://ifeve.com/buffers/ BIO/NIO/AIO的区别联系 http://stevex.blog.51cto.com/4300375/1284437http://w ...
- $rootScope
参考地址 https://docs.angularjs.org/api/ng/type/$rootScope.Scope angular 的scope一般上都是有继承关系的,也就是说可以通过原型访问到 ...
- SeekBar.OnSeekBarChangeListener解析
public static interface SeekBar.OnSeekBarChangeListener android.widget.SeekBar.OnSeekBarChangeListen ...
- MySQL的SQL_CALC_FOUND_ROWS真的很慢么?
分页程序一般由两条SQL组成: SELECT COUNT(*) FROM ... WHERE .... SELECT ... FROM ... WHERE LIMIT ... 如果使用SQL_CALC ...
- Linux watch 命令
man watch: WATCH(1) Linux User's Manual WATCH(1) NAME watch - execute a program periodically, showin ...
- poj1220:高精度进制转换模板题
今天撸3708 一直奇怪的re 就先放下了,写这个题的过程中学习了一个高精度进制转换,用这个模板写了1220 记录一下: #include <iostream> #include < ...
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- 使用教程 - BestSync同步软件 - SQL2008R2 数据库定时备份解决方案
需求: 1. 某公司的管理软件,数据库为SQL2008R2.2. 将整个数据库作为一个文件,定时同步到FTP 服务器3. 需要有多个备份,每同步一次,都备份上次的文件到备 ...
- loadrunner java 缺少必要的导入包报错
loadrunner 运行从eclipse中做好的脚本,ctrl + A 复制到loadrunner中来, 添加参数化的的语句:verifyCode = lr.eval_string (&quo ...