python3学习笔记---文件和异常

文件处理:能够快速分析大量的数据

错误处理:避免程序再面对意外情形时崩溃

异常:python创建的特殊对象,用于管理程序运行时出现的错误

json模块:保存用户数据,以免再程序停止运行后丢失。

1、从文件中读取数据

根据文件名称打开文件:(前提:被打开文件位于程序文件所属目录

with open("pi_digits.txt") as file_object:  ###pi_digits.txt文件名, 文件对象保存在变量file_object中
    contents = file_object.read()           ###读取文件内容,放入变量contents中
    print(contents)                         ###打印文件内容

open()函数:

接受一个参数:要打开的文件名称。

返回一个表示文件的对象,并将其存储在后面使用的变量中。

根据文件路径打开文件:

要打开一个不与程序文件位于同一个目录中的文件,需要提供文件路径,它让python到指定的位置去查找。

相对路径:让python到指定的位置去查找,而该位置是相对于当前运行的程序所在目录的。

假设程序文件存储在文件夹pyhton_work中,被打开的文件filename.txt在pyhton_work文件夹下的一个子文件夹text_files内。

Linux和OS系统中:

with open('text_files/filename.txt') as file_object:

Windows系统:

with open('text_files\filename.txt') as file_object:

绝对路径:可读取系统任何地方的文件。将绝对路径存储早一个变量中,再将该变量传递给open()

Linux和OS系统中:

file_path = '/home/ehmatthes/other_files/text_files/filename.txt'
with open(file_path) as file_object:

Windows系统:

file_path = r'C\Users\ehmatthes\other_files\text_files\filename.txt'
with open(file_path) as file_object:

目前最简单的做法:要么将数据文件存储在程序文件所在的目录,要么将其存储在程序文件所在录下的一个文件夹(如text_files)中。

注意:Python中反斜杠呗视为转义字符,为在Windows系统中确保万无一失,应以原始字符串的方式定义指定路径,即在开头的单引号前加上r

逐行读取:

filename = 'pi_digits.txt'
with open(filename) as file_object:
    for line in file_object:   ##逐行读取,for循环实现
        print(line)

创建一个包含文件各行内容的列表:

使用关键字with时,open()返回的文件对象只在with代码块可用。

如果要在with代码块外访问文件的内容,可在with代码块内将文件的各行存储在一个列表中。

并在with代码块外使用该列表:你可以立即处理文件的各个部分,也可推迟到程序后再处理。

使用文件内容:

Python将其中的所有文本都解读为字符串。如果你读取的是数字,并要将其作为数值使用,必须使用函数int()将其转换为整数,或使用函数float转换为浮点数。

filename = 'pi_digits.txt'

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

pi_string  = ''
for line in lines:
    pi_string += line.rstrip()   #删除每行末尾的换行符

print(pi_string)
print(len(pi_string))       #打印字符串及其长度

圆周率包含你生日吗?

编写程序确定你的生日是否包含在圆周率的100万位中。将生日表示为一个由数字组成的字符串,检查这个字符串是否包含在pi_string中。

filename = "pi_million_digits.txt"

with open(filename) as file_object:
    lines = file_object.readlines()
    pi_string = ""
    for line in lines:
        pi_string += line.strip()
        
        
    birthday = input("Enter your birthday, in the form mmddyy:")
    if birthday in pi_string:
        print("Your birthday appears in the first million digits of pi!")
    else:
        print("Your birthday doesn't appears in the first million digits of pi!")

2、写入文件

example:

filename = 'programming.txt'

with open(filename, 'w') as file_object:
    file_object.write("I love Kris!")

示例中,open文件名称打开模式)函数提供了两个实参。

 读取模式:“r”     

 写入模式:“w”   #以写入模式打开时,如果文件按已经存在,python将在返回文件对象前清空该文件。

 附加模式:“a”   #Python不会再返回文件对象前清空文件,而你写入到文件的行都将添加到文件末尾。

                           #如果指定的文件不存在,Python将为你创建一个空文件。

读取和写入模式:“r+”

#如果要打开的文件不存在,函数open()自动创建它。

#Python只能将字符串写入文本文件。要将数值数据存储到文本文件,必须首先使用函数str()转换位字符串格式。

#write()函数写入多行时不会添加换行符,写入的数据会全部挤在同一行,需手动添加换行符(\n)

3、异常

异常:管理程序执行期间发生错误的特殊对象。

如果编写了一个处理该异常的代码,程序将继续运行;如果未对异常进行处理,程序将停止,并显示一个traceback,其中包含有关异常的报告。

异常使用try-except 代码块进行处理:通过将可能引发错误的代码放在try-except 代码块中,可提高这个程序抵御错误的能力。

else代码块:依赖于try代码块成功执行的代码都应放到else代码块中。

try:
    可能引发异常的代码
except 异常类型名称:
    引发异常时,要运行的代码
else:
    try代码块运行正常时运行的代码

使用异常避免崩溃:

下面来创建一个只执行除法运算的简单计算器:

print("Give me two numbers, and I'll divide them.")
print("Enter 'q' to quit")

while(True):
    first_number = input("\nFirst_number: ")
    if first_number == 'q':
        break
    second_number = input("\nSecond_number:")
    if second_number == 'q':
        break
    answer = int(first_number)/int(second_number)
    print(answer)

>>>

Give me two numbers, and I'll divide them.
Enter 'q' to quit

First_number: 5

Second_number:0
Traceback (most recent call last):
  File "C:/Users/18969/Desktop/hahh/xx.py", line 13, in <module>
    answer = int(first_number)/int(second_number)
ZeroDivisionError: division by zero

#当第二个数字为0时,程序将会崩溃。

使用try-except代码块进行改善:

print("Give me two numbers, and I'll divide them.")
print("Enter 'q' to quit")

while(True):
    first_number = input("\nFirst_number: ")
    if first_number == 'q':
        break
    second_number = input("\nSecond_number:")
    if second_number == 'q':
        break
    else:
        try:
            answer = int(first_number)/int(second_number)
        except ZeroDivisionError:
            print("You can't divided by 0!")
        else:
            print(answer)

>>>

First_number: 5

Second_number:0
You can't divided by 0!

First_number: 10

Second_number:1
10.0

#添加蓝色字体部分后,发送给用户一条友好的消息,告诉用户如何避免这种错误。程序也可以继续运行。

通过预测可能发生错误的代码,可编写健壮的程序,他们即便面临无效数据或缺少资源,也能继续运行,从而能抵御无意的用户错误和恶意的攻击。

分析文本:

假如分析一本书的文本文件,很多经典的文学作品都是以简单的文本文件形式提供,因为他们不受版权限制。

Gutenberg项目(http://www.gutenberg.org/)包含了超过57000本电子书.

当要打开的文件不存在时,同样可使用try-except代码块处理异常。

filename = 'alice.txt'

try:
    with open(filename) as file_object:
        contents = file_object.read()
except FileNotFoundError:     #当文件不存在时,会发生异常
    print("The file " + filename +" doesn't exist!")
else:
    #计算文件大致包含对少单词
    words = contents.split()
    number_word = len(words)
    print("The file " + filename + str(number_word) + " words.")

>>>

The file alice.txt has about 29461 words.

当处理多个文件时,没有异常处理操作时,一个文件不存在,其余的文件也无法执行相应的程序。下面代码分析多个文本:

def count_words(filaname):
"""计算一个文件包含多少个单词"""
    try:
        with open(filename) as file_object:
            contents = file_object.read()

    except FileNotFoundError:
        msg = "Sorry, the file " + filename + "doesn't not exist."
        print(msg)

    else:
        #计算大致包含多少个单词
        words = contents.split()
        number_words = len(words)
        print("The file " + filename +" has about " + str(number_words) + " words")


filenames = ['alice.txt', 'siddhartha.txt', 'moby_dick.txt', 'Little_women.txt']
for filename in filenames:
    count_words(filename)

这个示例中,使用try-except代码块提供了两个重要的优点:】

①避免让用户看到traceback;

②让程序能够继续分析能够找到其他的文件。

失败时一声不吭:

如果希望程序发生异常时一声不吭,就像什么都没有发生一样继续运行。

要让程序再失败时一声不吭,可像通常那样编写try代码块,但在except代码块中明确地告诉Python很美都不要做。Python有一个pass语句,可在代码块中使用它来让Python什么都不要做。

try:
    可能引发异常的代码
except 异常类型名称:
    pass
else:
    try代码块运行正常时运行的代码

4、存储数据

JSON模块让你能够将简单地Python数据结构转储到文件中,并在程序再次运行时加载该文件中的数据。你还可以使用JSON在python程序之间分享数据。JSON数据格式非Python专用,以JSON格式存储的数据可与其他编程语言的人分享。

#JSON(JavaScript Object Notation)格式最初是为JavaScript开发,但随后成立一种常见的格式,被众多语言采用。

json.dump(存储的数据,存储数据的文件对象):存储数据

json.load(存储数据的文件对象):将数据读取到内存中的程序

示例:

import json

number = [1,2,3,4,5,6,7,8,9,0]

filename = 'numbers.json'
with open(filename, 'a') as file_object:
    json.dump(number, file_object)  #number:存储的数据  file_object:存储数据的文件对象

number.json文件中数据的存储格式与Python一样:

>>>[1,2,3,4,5,6,7,8,9,0]

下面再将number.json文件中的数据读取出来"

import json

filename = 'numbers.json'
with open(filename) as file_object:
    number = json.load(file_object)
print(number)

>>>[1,2,3,4,5,6,7,8,9,0]

保存和读取用户生成的数据:

示例:
import json

username = input('What is your name?')

filename = 'username.json'
with open(filename, 'w') as file_object:
    json.dump(username, file_object)
    print("We will remember when you come back, " + username + '!')

>>>

What is your name?Kris
We will remember when you come back, Kris!

再编写一个程序,向其中被存储的用户发出问候:

import json

filename = 'username.json'
with open(filename) as file_object:
    username = json.load(file_object)
    print("Welcome back! " + username + '!')

>>>

Welcome back! Kris!

将两个程序合并到一个程序:

import json

#如果以前存储了用户名,就加载它
#否则, 就提示用户输入用户名单并存储它
filename = 'username.json'
try:
    with open(filename) as file_object:
        username = json.load(file_object)
except FileNotFoundError:
    username = input("What's your name? ")
    with open(filename, 'w') as file_object:
        json.dump(username, file_object)
        print("We will remember you when you come back, " + "!")

else:
    print("Welcome back, " +  username +"!")

>>>

Welcome back, Kris!

重构:

将代码划分为一系列完成具体工作的函数。这样的过程称为重构。

重构让代码更清晰、更易于理解、更容易扩展。

下面的函数greet_user()大部分逻辑都放在一个函数中,重构前代码如下:

import json

def greet_user():
     """问候用户,并指出名字"""
     filename = 'username.json'
     try:
         with open(filename) as file_obj:
            username = json.load(file_obj)
     except FileNotFoundError:
         username = input("What's your name? ")
         with open(filename, 'w') as file_obj:
            json.dump(username, file_obj)
         print("We will remember you when you back," + username + "!")

     else:
         print("Welcome back, " + username + "!")


greet_user()

>>>Welcome back, Kris!

重构后的程序如下:

import json

def get_stored_username():
    """如果存储了用户名,就获取它"""
    filename= 'username.json'
    try:
        with open(filename) as file_obj:
            username = json.load(file_obj)
    except FileNotFoundError:
        return None
    else:
        return username


def get_new_username():
    """提示用户输入用户名"""
    username = input("What's your name? ")
    filename = 'username.json'
    with open(filename,'a') as file_obj:
        json.dump(username, file_obj)
    return username


def  greet_user():
    """问候用户,并指出姓名"""
    username = get_stored_username()
    if username:
        print("Welcome back, " + username+ " !")
    else:
        username = get_new_username()
        print("We will remember you when you come back, " + username + "!")


greet_user()

>>>Welcome back, Kris!

猜你喜欢

转载自blog.csdn.net/qq_33690342/article/details/82662262