# 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)的更多相关文章

  1. 数值运算内建函数(core python programming 2nd edition 5.6.2)

    数值运算内建函数 函数  功能 abs(num) 返回 num 的绝对值 coerce(num1, num2) 将num1和num2转换为同一类型,然后以一个元组的形式返回. divmod(num1, ...

  2. Core Python Programming一书中关于深浅拷贝的错误

    该书关于深浅拷贝的论述: 6.20. *Copying Python Objects and Shallow and Deep Copies "when shallow copies are ...

  3. [core python programming]chapter 7 programming MS office

    excel.pyw会有问题,解决如下: 因为python3x中没有tkMessageBox模块,Tkinter改成了tkinter你可以查看你的py当前支持的模块.在交互式命令行下输入>> ...

  4. 开始 python programming第三版案例分析

    最近研究python,打算将python programming第三版案例分析下 但是全书1600多页 比较费时 而且 介绍太多 感觉没有必要! python programming 堪称经典之作 第 ...

  5. Entity Framework 6 Recipes 2nd Edition 译 -> 目录 -持续更新

    因为看了<Entity Framework 6 Recipes 2nd Edition>这本书前面8章的翻译,感谢china_fucan. 从第九章开始,我是边看边译的,没有通读,加之英语 ...

  6. 第十四周翻译-《Pro SQL Server Internals, 2nd edition》

    <Pro SQL Server Internals, 2nd edition> 作者:Dmitri Korotkevitch 翻译:赖慧芳 译文: 设计和优化索引 定义一种应用于所有地方的 ...

  7. Learning ROS for Robotics Programming - Second Edition(《学习ROS机器人编程-第二版》)

    Learning ROS for Robotics Programming - Second Edition <学习ROS机器人编程-第二版> ----Your one-stop guid ...

  8. Learning ROS for Robotics Programming Second Edition学习笔记(十) indigo Gazebo rviz slam navigation

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 moveit是书的最后一章,由于对机械臂完全不知,看不懂 ...

  9. Learning ROS forRobotics Programming Second Edition学习笔记(八)indigo rviz gazebo

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS forRobotics Pro ...

随机推荐

  1. Simple Event Correlation installation and configuration

    http://searchenterpriselinux.techtarget.com/tip/Simple-Event-Correlation-installation-and-configurat ...

  2. qtpanel

    https://github.com/MadFishTheOne/qtpanel https://github.com/xiangzhai/qtpanel

  3. 开源的Owin 的身份验证支持 和跨域支持

    http://identitymodel.codeplex.com/ https://identityserver.github.io/ Windows Identity Foundation 6.1 ...

  4. 【Android】通过Java代码替换TabHost中的drawableTop资源

    在博客 http://blog.csdn.net/jueblog/article/details/11837445 中的Tab选项卡中, 点击相应的Tab选项,图标没有发生改变. 这些资源图片也没有尽 ...

  5. URAL 1029

    题目大意:M层N列的矩阵(各元素均为正整数),找出一个路径从第一层到达第M层,使得路径上的所有数的和是所有可达路径中最小的,每次上到下一层以后就不能再上去,依次输出路径上的各点在所在层的列数. KB  ...

  6. linux 版本家族

    1. 简单的说,在桌面系统上,可分为Debian和RedHat两大分支,然后Debian这一分支到现在比较火的是Ubuntu, RedHat比较火的是Fedora.贴一下它们的版本历史:  fedor ...

  7. 【转】64位win7硬盘安装64位ubuntu 13.04

    原文网址:http://www.cnblogs.com/jiangz/p/3751617.html 最近本来是准备通过升级的方式把ubuntu从12.04升级到12.10再升级到13.04的,但是升级 ...

  8. Linux系统下C++开发工具-远程终端软件使用

    通过前面安装Linux系统介绍,紧接着进入搭建Linux开发环境的第二步,选择C++开发工具,以及必要的客户端软件.从而完整的搭建一个Linux下C++开发的环境,便于初学者在该环境下能够很好的学习. ...

  9. Servlet实现文件的下载

    (1)项目的主文件夹例如以下:(演示出image和servlet的位置所在) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGxnZW4xNTczODc= ...

  10. hdu4000 &amp;&amp; hrbust1625

    ikki的数字 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 22(12 users) Total Accepted: 9(8 use ...