第一次碰到try-except(core python programming 2nd Edition 3.6)

 1 # coding: utf-8 # 使用Windows系统,首行‘#!/usr/bin/env Pyton‘无用,全部改为‘# coding: utf-8‘
 2
 3 ‘readtextfile.py -- read and display text file‘
 4
 5 # get filename
 6 fname = raw_input(‘Enter filename: ‘)
 7 print
 8
 9 # attempt to open file for reading
10 try:
11     fobj = open(fname, ‘r‘)        # 尝试打开fname
12 except IOError, e:         # 捕获到的IOError错误的详细原因会被放置在对象e中,然后运行该异常的except代码块(也可以写成‘except IOError as e:‘)
13     print "*** file open error:", e     # 打印异常信息
14 else:                             # 如果try没有错误,执行else语句
15     # display contents to the screen
16     for eachLine in fobj:
17         print eachLine,             # 逗号是为了抵制print语句自动生成的行结束符(fname里面本身已有行结束符,如果不加逗号,打印出来的内容之间会多空一行)
18     fobj.close()
与Python异常相关的关键字:
关键字          关键字说明
raise           抛出/引发异常
try/except      捕获异常并处理
pass            忽略异常
as              定义异常实例(except IOError as e)
finally         无论是否出现异常,都执行的代码
else            如果try中的语句没有引发异常,则执行else中的语句
时间: 2024-11-03 03:24:43

第一次碰到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, num2) 除法-取余运算的结合.返回一个元组(num1/num2,num1 %num2).对浮点数和复数的商进行下舍入(复数仅取实数部分的商). pow(num1, num2, mod=1) 取num1 的num2次方,如果提供mod参数,则计算结果再对mod进行取余运算. round(flt,

[core python programming]chapter 7 programming MS office

excel.pyw会有问题,解决如下: 因为python3x中没有tkMessageBox模块,Tkinter改成了tkinter你可以查看你的py当前支持的模块.在交互式命令行下输入>>> help()>>> modules 前三行改为: 1 from tkinter import Tk 2 from time import sleep 3 from tkinter.messagebox import showwarning

Objective-C Programming (2nd Edition)

Book Description Want to write iOS apps or desktop Mac applications? This introduction to programming and the Objective-C language is your first step on the journey from someone who uses apps to someone who writes them. Based on Big Nerd Ranch's popu

Core Bluetooth Programming Guide - Performing Common Peripheral Role Tasks 粗译

Core Bluetooth Programming Guide 粗译 续 code {padding:0.2em 0.4em; background:#e1e9ed;} pre {text-align:left; overflow-x: scroll; color: #d3bd7e; background: #202020; padding: 10pt 15pt; border-radius: 3px;} hr {border: 0px; border-top: 1px solid #ddd;

Master the 10 Most Common Python Programming Problems - 10大最常见的Python编程错误

http://blog.csdn.net/pipisorry/article/details/45175457 Introduction 本文介绍python编程中很难捕捉10大错误 (Note: This article is intended for a more advanced audience than Common Mistakes of Python Programmers, which is geared(适合) more toward those who are newer t

核心动画编程指南Core Animation Programming Guide - iOS

1 有关核心动画 1.1 概览 1.1.1 Core Animation 管理应用的内容 核心是 layer objects, 1.1.2 更改 layer 触发动画 Like views, layer objects have a bounds rectangle, a position onscreen, an opacity, a transform, and many other visually-oriented properties that can be modified. 更改这

十五周翻译《Pro SQL Server Internals, 2nd edition》 CHAPTER 7

文章来自<Pro SQL Server Internals, 2nd edition>的CHAPTER 7 Designing and Tuning the Indexes中的Clustered Index Design Considerations> 原作者:Dmitri Korotkevitch 第七章  设计和调整索引 无法定义可在任何地方使用的索引策略. 每个系统都是独特的,需要它自己的索引方法基于工作量,业务需求和其他一些因素.但是,有几个设计考虑因素和指南可以应用于每个系统.

翻译:《Pro SQL Server Internals, 2nd edition》CHAPTER 7 Designing and Tuning the Indexes

文章选自:<Pro SQL Server Internals, 2nd edition>CHAPTER 7 Designing and Tuning the Indexes中的Clustered Index Design Considerations一节 作者:Dmitri Korotkevitch Clustered Index Design Considerations 聚集索引设计注意事项 每次更改集群索引键的值时,都会发生两件事.首先,SQL Server移动行到群集索引页链和数据文件

Elasticsearch Server,2nd Edition pdf 翻译 中文

很偶然的机会,就需要接触到搜索,入门就是google trend已然超过solr的ES.在入门的时候找书的时候发现没有中文版的.于是自己开始翻译Elasticsearch Server,2nd Edition.应该是全球头一份了.看到微博上已经有出版社把英文书引过来,刚找到了人翻译这本书.我就先放出来自己翻的一部分.如果他真找到这里来了,就义务提供一下自己翻译的资源: 翻译是同步在我的Evernote里.贴出来到blog里样式都乱了.索性分享出来我的Evernote.去看去吧. 如果要引用或者转