文件和异常
处理文件;
错误处理,避免程序在面对意外情形时崩溃;
处理异常;
模块json,能够保存用户数据,以免在程序停止运行后丢失。
一.从文件中读取数据
使用文本中的信息,首先要将信息读取到内存中。
可以一次性读取文件中的全部内容,也可以每次一行的方式逐步读取。
1.读取整个文件
with open('text_files\data.txt') as file_object:
contens = file_object.read()
print(contens.rstrip())
Today is sunday.
Weather is fine.
open()接受一个参数:要打开文件的名称,
关键字with在不需要访问文件后将其关闭,
read()读取这个文件的所有内容,并返回一个字符串存储在变量contents中。
rstrip()删除字符串末尾的空白。
2.文件路径
python在当前执行的文件(即.py程序文件)所在的目录中查找文件。
要让python打开与程序文件不在同一目录中的文件,需要提供文件路径。
相对文件路径
文件夹text_files在文件夹python_work中,使用相对文件路径来打开该文件夹中的文件,该位置是相对当前运行的程序所在目录的。
‘text_files\data.txt’
绝对文件路径
完整路径。
简单做法
要么将数据文件存储在程序文件所在的目录,直接使用数据文件名;
要么将其存储在程序文件所在目录下的一个文件夹,使用相对路径。
3.逐行读取
with open('text_files\data.txt') as file_object:
for line in file_object:
print(line.rstrip())
Today is sunday.
Weather is fine.
4.创建一个包含文件各行内容的列表
with open('text_files\data.txt') as file_object:
lines = file_object.readlines()
print(lines)
for line in lines:
print(line.rstrip())
['Today is sunday.\n', 'Weather is fine.']
Today is sunday.
Weather is fine.
使用关键字with,open()返回的文件对象只在with代码块内可用。
readlines()从文件中读取每一行,并将其存储在一个列表中,该列表存储到变量lines中,在with代码块外,依然可以使用这个变量。
5.使用文件的内容
with open('text_files\data.txt') as file_object:
lines = file_object.readlines()
pi_strings = ''
for line in lines:
pi_strings+=line.rstrip()
print(pi_strings)
print(len(pi_strings))
Today is sunday.Weather is fine.
32
读取文本文件时,python将其中的所有文本都解读为字符串。如果读取的是数字,需要将其作为数值使用,必须用函数int()将其转化为整数,或者用float()将其转化为浮点数。
二.写入文件
1.写入空文件
将文本写入文件,在调用open()时需要提供另一个实参。
filename="programming.txt"
with open(filename,'w') as file_object:
file_object.write("I love programming.")
读取模式r
写入模式w
附加模式a
读取和写入模式r+
如果省略了模式实参,将以默认的只读模式打开文件。
如果要写入的文件不存在,python将自动创建它。
以写入模式w打开文件时,如果指定的文件已经存在,python将在返回文件对象前清空该文件。
**python只能将字符串写入文本文件,要将数值存储到文本文件中,必须先使用str()将其转化为字符串格式。
2.写入多行
函数write()不会在写入的文本末尾添加换行符。写入多行时需要指定换行符。
filename="programming.txt"
with open(filename,'w') as file_object:
file_object.write("I love programming.\n")
file_object.write("I love creating new game.\n")
I love programming.
I love creating new game.
3.附加到文件
给文件添加内容,而不是覆盖原有内容,可以用附加模式a打开文件,写入到文件的行都将添加到文件末尾。
如果指定的文件不存在,python将创建一个空文件。
filename="programming.txt"
with open(filename,'a') as file_object:
file_object.write("I also love finding meaning in large datasets.\n")
file_object.write("I love creating apps that can run in a browser.\n")
I love programming.
I love creating new game.
I also love finding meaning in large datasets.
I love creating apps that can run in a browser.
三.异常
python使用被称为异常的特殊对象来管理程序执行期间发生的错误。
发生错误时,如果未对异常进行处理,程序将停止,并显示一个traceback,其中包含异常的报告;如果编写了处理该异常的代码,程序将继续运行。
异常是使用try-except代码块处理的。
使用这个代码块时,即使出现异常,程序也将继续运行。
1.ZeroDivisionError异常
try-except-else代码块
print("Give me two numbers,and I'll divide them
("Enter 'q' to quit.")
while True:
first_number = input('\nFirst number:')
if first_number == 'q':
break
second_number = input("Second number:")
if second_number == 'q':
break
try:
answer = int(first_number)/int(second_number)
except ZeroDivisionError:
print("You can't divide by zero!")
else:
print(answer)
Give me two numbers,and I'll divide them.
Enter 'q' to quit.
First number:9
Second number:0
You can't divide by zero!
First number:8
Second number:2
4.0
First number:q
只有可能引发异常的代码块才需要放在else代码块中。一些仅在try代码块成功运行时才需要运行的代码,放在else代码块中。
2.FileNotFoundError异常
filename='alice.txt'
try:
with open(filename) as f_obj:
contens=f_obj.read()
except FileNotFoundError:
msg = "Sorry,the file " + filename + " does not exist."
print(msg)
Sorry,the file alice.txt does not exist.
最终结果是显示一条友好的错误信息,而不是traceback。
3.分析文本
计算文件大致包含多少个单词
filename='programming.txt'
try:
with open(filename) as f_obj:
contens=f_obj.read()
except FileNotFoundError:
msg = "Sorry,the file " + filename + " does not exist."
print(msg)
else:
words = contens.split()
num_words = len(words)
print("the file " + filename + " has about " + str(num_words) + " words.")
the file programming.txt has about 26 words.
split()默认以空格为分隔符将字符串拆成多个部分,并返回一个包含字符串中所有单词的列表。
4.使用多个文件
def count_words(filename):
"""计算一个文件大致包含多少文件"""
try:
with open(filename) as f_obj:
contens=f_obj.read()
except FileNotFoundError:
msg = "Sorry,the file " + filename + " does not exist."
print(msg)
else:
words = contens.split()
num_words = len(words)
print("the file " + filename + " has about " + str(num_words) + " words.")
filenames = ['alice.txt','programming.txt','moby_dick.txt','little_wowen.txt']
for filename in filenames:
count_words(filename)
Sorry,the file alice.txt does not exist.
the file programming.txt has about 26 words.
Sorry,the file moby_dick.txt does not exist.
Sorry,the file little_wowen.txt does not exist.
两个优点:
避免让用户看到traceback
程序能够继续分析能够找到的其他文件。
5.pass语句
在程序发生异常时一声不吭,在except代码块中使用pass语句,让python什么都不要做。
excpet FileNotFoundError:
pass
四.存储数据
模块json能够将简单的python数据结构转储到文件中,并在程序再次运行时加载该文件中的数据。
使用json在python程序之间分享数据。
json数据格式并非python专用的。
1.使用json.dump()和json.load()
函数json.dump()接受两个参数,要存储的数据和
可用于存储的文件对象。
用json.dump()来存储数字列表
import json
numbers=[2,3,5,7,11,13]
filename = 'number.json'
with open(filename,'w') as f_obj:
json.dump(numbers,f_obj)
使用文件扩展名来指出文件存储的数据为json格式,以写入模式打开这个文件,让json能够将数据写入其中。
打开文件numbers.json,内容为[2,3,5,7,11,3]
使用json.load()将这个列表读取到内存中
import jsonfilename = "numbers.json"
with open(filename) as f_obj:
numbers = json.load(f_obj)
print(numbers)
[2, 3, 4, 5, 6, 7]
2.保护和读取用户生成的数据
对于用户生成的数据,如果不以某种方式进行存储,程序停止时用户的信息将丢失。
import json
#如果以前存储了用户名,就加载它;否则提示用户输入用户名并存储它
filename = 'username.json'
try:
with open(filename) as f_obj:
uesrname=json.load(f_obj)
except FileNotFoundError:
uesrname = input("What is your name?")
with open(filename,'w') as f_obj:
json.dump(uesrname,f_obj)
print("We'll remember you when you come back, " + uesrname + "!")
else:
print("Welcome back, " + username)
What is your name?Eric
We'll remember you when you come back, Eric!
3.重构
将代码划分为一系列完成具体工作的函数,这样的过程成为重构。
import json
def get_stored_username():
"""如果存储了用户名,就获取它"""
filename = 'username.json'
try:
with open(filename) as f_obj:
username=json.load(f_obj)
except FileNotFoundError:
return None
else:
return username
def greet_user():
"""问候用户,并指出其名字"""
uesrname = get_stored_username()
if uesrname:
print("Welcome back, " + uesrname + "!")
else:
uesrname = input("What is your name?")
with open(filename,'w') as f_obj:
json.dump(uesrname,f_obj)
print("We'll remember you when you come back, " + uesrname + "!")
greet_user()
Welcome back, Eric!
代码清晰而易于维护和扩展。