第一次碰到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 ...
随机推荐
- Python学习笔记总结(三)类
一.类简单介绍 1.介绍 类是Python面向对象程序设计(OOP)的主要工具,类建立使用class语句,通过class定义的对象. 类和模块的差异,类是语句,模块是文件. 类和实例 实例:代表程序领 ...
- 安卓仿制新浪微博(一)之OAuth2授权接口
这里需要用到请求授权(authorize)以及获取授权(access_token) 第一步: 将新浪的sdk放在src/libs下面 二: //创建方法实现authorize public void ...
- LeetCode_Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- Windows脚本 - %~dp0的含义
含义是:更改当前目录为批处理本身的目录,有些晕吧?不急,我举例 比如你有个批处理a.bat在D:\qq文件夹下 a.bat内容为 cd /d %~dp0 在这里,cd /d %~dp0的意思就是cd ...
- Sessions, Window Stations and Desktops(GetDesktopWindow函数得到的桌面句柄, 是Csrss.exe创建的一个窗口)
由于不是搞安全的,对这块的东西一直不是很清楚,今天特意整理下. 总的来说,他们的关系如下: 我们知道Windows是支持多用户的,Session可以理解为每个用户登录,比如当前登录和远程登陆.Wind ...
- MFC消息映射与命令传递
题外话:刚开始学视窗程序设计的时候,我就打印了一本Windows消息详解,里面列举了各种已定义消息的意义和作用,共10多页,在编程的时候翻翻,有时觉得很受用.我发觉很多编程的朋友,虽然每天都面对消息, ...
- (Data structure)Implement Trie && Add and Search Word
Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...
- Java实现一致性Hash算法深入研究
一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细解读一文中”一致性Hash算法”部分,对于为什么要使用一致性Hash算法和一致性Hash算法的算法原 ...
- Matlab画图常用的符号和颜色
线型 说明 标记符 说明 颜色 说明 - 实线(默认) + 加号符 r 红色 -- 双划线 o 空心圆 g 绿色 : 虚线 * 星号 b 蓝色 :. 点划线 . 实心圆 c 青绿色 x 叉号符 m 洋 ...
- [转]使用Composer管理PHP依赖关系
简介 现在软件规模越来越大,PHP项目的开发模式和许多年前已经有了很大变化.记得初学PHP那会儿,boblog是一个很好的例子,几乎可以代表 PHP项目的开发模式.当时PHP 5.x以上的版本刚开始流 ...