chapter10 文件和异常
10.1 从文件中读取数据
10.1.1 读取整个文件
with open("pi.txt") as file_object:
contents = file_object.read()
print(contents)
print(contents.rstrip())//去掉空白行
10.1.2 文件路径
10.1.3 逐行读取
filename = 'pi.txt'
with open(filename) as file_object:
for line in file_object:
print(line)
10.1.4 创建一个包含文件各行内容的列表
使用with关键字,open()返回的文件对象只在with代码块内可用。
filename = 'pi.txt'
with open(filename) as file_object:
lines = file_object.readlines()
for line in lines:
print(line.tstrip())
10.1.5 使用文件的内容
删除每行末尾的换行符 -> rstrip()
删除每行左边的空格 -> strip()
10.1.6 包含一百万位3的大型文件
filename = 'pi.txt'

with open(filename) as file_object():
lines = file_object.readlines()

pi_string =''
for line in lines:
pi_string += line.strip()

print(pi_string[:52] + "...")
print(len(pi_string))
10.1.7 圆周率值中包含你的升入吗
birthday = input("Enter your birthday,in the form mmddyy:")
if birthday in pi_string:
print("OK")
else:
print("No")
10.2 写入文件
10.2.1 写入空文件
filename = "programming.txt"

with open(filename,'w') as file_object:
file_object.write("I love programming")
10.2.2 写入多行
10.2.3 附加到文件
10.3 异常
10.3.1 处理ZeroDivisionError异常
10.3.2 使用try-except代码块
try:
print(5/0)
except ZeroDivisonError:
print("You can't divide by zero")
10.3.3 使用异常避免崩溃
10.3.4 else 代码块
try:
answer = int(first_number)/int(second_number)
exception ZeroDivisionError:
print("You can't divide by 0!")
else:
print(answer)
10.3.5 处理FileNoFoundError异常
filename = "alice.txt"

try:
with open(filename) as f_obj:
contents = f_obj.read()
excep FileNotFoundError:
msg = "Sorry, the file " + filename + " does not exit"
print(msg)
10.3.6 分析文本
split() //根据空格分割字符串到列表
10.3.7 使用多个文件
10.3.8 失败时一声不吭
try:
//...
catch FileNotFoundError:
pass
else:
--snip--
10.4 存储数据
10.4.1 使用json.dump()和json.load()
//保存到文件
import json

numbers = [2,3,4,5,6,6]

filename = 'numbers.json'
with open(filename,'w') as file_object:
json.dump(numbers,file_object)

//读取到内存
import json

filename = 'numbers.json'
with open(filename) as file_object:
numbers = json.load(file_object)
print(numbers)
10.4.2 保存和读取用户生成的数据
10.4.3 重构

【Python编程:从入门到实践】chapter10 文件和异常的更多相关文章

  1. Python编程从入门到实践笔记——文件

    Python编程从入门到实践笔记——文件 #coding=gbk #Python编程从入门到实践笔记——文件 #10.1从文件中读取数据 #1.读取整个文件 file_name = 'pi_digit ...

  2. 《Python编程从入门到实践》_第十章_文件和异常

    读取整个文件 文件pi_digits.txt #文件pi_digits.txt 3.1415926535 8979323846 2643383279 下面的程序打开并读取整个文件,再将其内容显示到屏幕 ...

  3. Python编程从入门到实践笔记——异常和存储数据

    Python编程从入门到实践笔记——异常和存储数据 #coding=gbk #Python编程从入门到实践笔记——异常和存储数据 #10.3异常 #Python使用被称为异常的特殊对象来管理程序执行期 ...

  4. Python编程从入门到实践笔记——类

    Python编程从入门到实践笔记——类 #coding=gbk #Python编程从入门到实践笔记——类 #9.1创建和使用类 #1.创建Dog类 class Dog():#类名首字母大写 " ...

  5. Python编程从入门到实践笔记——函数

    Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...

  6. Python编程从入门到实践

    Python编程从入门到实践1 起步2 变量和简单数据类型3 列表简介4 操作列表5 if语句6 字典7 用户输入和while循环8 函数9 类10 文件和异常11 测试代码12 武装飞船13 外星人 ...

  7. 《Python编程:从入门到实践》分享下载

    书籍信息 书名:<Python编程:从入门到实践> 原作名:Python Crash Course 作者: [美] 埃里克·马瑟斯 豆瓣评分:9.1分(2534人评价) 内容简介 本书是一 ...

  8. 《python编程从入门到实践》读书实践笔记(二)

    本文是<python编程从入门到实践>读书实践笔记11章的内容,主要包含测试,为体现测试的重要性,独立成文. 11 测试代码 写在前面的话,以下是我这些年开发中和测试相关的血泪史. 对于一 ...

  9. 《python编程从入门到实践》读书实践笔记(一)

    本文是<python编程从入门到实践>读书实践笔记1~10章的内容,主要包含安装.基础类型.函数.类.文件读写及异常的内容. 1 起步 1.1 搭建环境 1.1.1 Python 版本选择 ...

  10. Python编程从入门到实践笔记——用户输入和while循环

    Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...

随机推荐

  1. MySQL性能优化方法四:SQL优化

    原文链接:http://isky000.com/database/mysql-performance-tuning-sql 注:这篇文章是以 MySQL 为背景,很多内容同时适用于其他关系型数据库,需 ...

  2. AppStore 中的app怎么样生成二维码,来提供下载

    首先在:iTunes里面找到  AppStore模块,然后搜索你的 App 在App 下载选项有 Copy Link ,拷贝地址 在二维码生成器里直接生成二维码就,OK 如果,想要多个和安卓做一个二维 ...

  3. iOS-----推送机制(上)

    推 送 机 制 使用NSNotificationCenter通信 NSNotificationCenter实现了观察者模式,允许应用的不同对象之间以松耦合的方式进行通信. NSNotification ...

  4. Generate And Play A Tone In Android hacking

    /*********************************************************************************** * Generate And ...

  5. Ubuntu忘记root密码的解决方法

    如果是Linux操作系统的话,其实也是很简单 -- 单用户登陆.下面以Ubuntu14.04来简单演示一下具体的操作流程. 1. 开机 2. 此时会有一个选项:Advanced Options for ...

  6. 谨慎使用 FileInfo.Exists 实例方法,而是使用 File.Exists 静态方法替代

    如果你在代码中使用了 FileInfo.Exists 实例方法来判断一个文件是否存在,也许会发现此方法可能错误地判断来一个文件是否真的存在.这是一个坑. 本文将介绍坑的原因,并提供填坑的办法. 本文内 ...

  7. 0-1背包 codeforces 55 D

    题目链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29608#problem/D 我把它化成了0-1背包,应该可以直接用多重背包做 ...

  8. 《DSP using MATLAB》Problem 4.18

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  9. benthos 通过rest api 配置 stream 说明

    stream 模式,我们可以通过rest api 进行控制 使用方法 启动 benthos --streams 进行流的配置(rest api) curl http://localhost:4195/ ...

  10. oracle mysql sql serve where in 语句的不同

    类似这样的语句在mysql  oracle 是可以执行成功的, select * from classfirst where (classid ,classname) not in (select c ...