美文网首页
【Python】10 文件和异常

【Python】10 文件和异常

作者: 吃葡萄的小兔纸 | 来源:发表于2019-08-25 22:35 被阅读0次

    前言:学习处理文件,让程序快速分析大量数据;学习异常,管理程序运营时出现的错误;学习模块json, 保存用户数据。

    10.1 文件读取

    10.1.1 读取整个文件

    with open('pi_digits.txt') as file_object: 
    #函数open() ;关键字with在不再需要访问文件后将其关闭;也可以调用open()和close()来打开和关闭;
        contents = file_object.read()#read()读取内容,并保存为字符串,末尾会返回空字符串;可以用rstrip()删除
        print(contents.rstrip())
    

    10.1.2 文件路径

    应用场景:要打开的文件不再程序文件所属目录中

    • 使用相对文件路径
    with open('text_files/filename.txt) as file_object: # linux和os系统; text_files 为文件夹名称
    with open('text_files\filename.txt) as file_object: # windows 系统
    
    • 使用绝对文件路径
    file_path = '/home/ehmatthes/other_files/text_files/filename.txt' #linux 和os系统
    with open(file_path) as file_object: 
    
    file_path = ' c:\home\ehmatthes\other_files\text_files\filename.txt' #win系统
    with open(file_path) as file_object: 
    

    10.1.3 逐行读取

    以每次一行的方式检查文件,可对文件对象使用for循环。

    filename = 'pi_digits.txt'
    with open(filename) as file_object:
        for line in file_object:
            print(line.rstrip())
    

    10.1.4 创建包含文件各行内容的列表

    filename = 'pi_digits.txt'
    with open(filename) as file_object:
        lines = file_object.readlines() #readlines()读取文件中的每一行,并将其存储在一个列表中
    
    for line in lines: #存储后可在with代码块之外使用
        print(line.rstrip())
    

    10.1.5 使用文件内容

    读取文本文件时,所有文本都解读为字符串。
    如要作为数值,使用函数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.strip()
    
    print(pi_string)
    print(len(pi_string))
    

    10.1.6 包含一百万位的大型文件

    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()
    
    print(pi_string[:52]+ '...') #只打印小数点后50位
    print(len(pi_string))
    

    10.1.7 检查文件时否包含数据或字符串

    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 does not appear in the first million digits of pi.')
    

    10.2 写入文件

    10.2.1 写入空文件

    Python只能写入字符串。如果存储数值,需先用函数str()将其转化为字符串格式。

    filename  = 'programming.txt'
    
    with open(filename.'w') as file_object:m#读取模式r,写入模式w,附加模式a,读取和写入r+;若要写入的文件不存在则自动创建
        file_object.write('i love programming.')# write()写入字符串
    

    10.2.2 写入多行

    函数write()不会在写入的文本末尾添加换行符,要让每个字符串都单独占一行,需要在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 games.\n")#增加换行符,使写入的文本单独一行
    

    10.2.3 附加到文件

    给文件添加内容而不覆盖原有内容,用附加模式打开文件。

    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")
    

    10.3 异常

    使用try-except代码块处理。

    10.3.1 处理zerodivisionerror异常

    try:
        print(5/0)
    except ZeroDivisionError:
        print('you cannot divide by zero!')
    

    10.3.2 使用异常避免崩溃

    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
        try:
            answer = int(first_number)/int(second_number)
        except ZeroDivisionError:
            print('you cannot divide by zero!')
        else:
            print(answer)
    

    10.3.5 处理filenotfounderror异常

    filename = 'alice.txt'
    
    try:
        with open(filename) as f:
            contents = f.read()
    except FileNotFoundError:
        msg = "sorry,the file "+filename +'does not exist.'
        print(msg)
    

    10.3.6 分析文本

    gutenberg 无版权限制的文学作品,可用来练习文本操作。
    方法split(),以空格为分隔符,根据一个字符串创建一个单词列表.

    filename = 'alice.txt'
    
    try:
        with open(filename) as f:
            contents = f.read()
    except FileNotFoundError:
        msg = "sorry,the file "+filename +'does not exist.'
        print(msg)
    else: #计算文件大致包含多少个单词
            words = contents.split()
            num_words = len(words)
            print("the file " + filename + " has about " + str(num_words) + ' words.')
    

    10.3.7 使用多个文件

    def count_words(filename):
        """计算一个文件大概包含多少单词"""
    
        try:
            with open(filename) as f:
                contents = f.read()
        except FileNotFoundError:
            msg = "sorry,the file "+filename +' does not exist.'
            print(msg)
        else: #计算文件大致包含多少个单词
            words = contents.split()
            num_words = len(words)
            print("the file " + filename + " has about " + str(num_words) + ' words.')
                
    filenames = ['alice.txt','siddhartha.txt','moby_dick,txt','little_women.txt']
    for filename in filenames:
        count_words(filename)
    

    10.3.8 pass 语句

    def count_words(filename):
        """计算一个文件大概包含多少单词"""
    
        try:
            with open(filename) as f:
                contents = f.read()
        except FileNotFoundError:
            pass # 出现异常时,直接执行except代码块中的语句
        else: #计算文件大致包含多少个单词
            words = contents.split()
            num_words = len(words)
            print("the file " + filename + " has about " + str(num_words) + ' words.')
                
    filenames = ['alice.txt','siddhartha.txt','moby_dick,txt','little_women.txt']
    for filename in filenames:
        count_words(filename)
    

    10.4 存储数据

    使用模块json(javascript object notation)存储数据

    10.4.1 使用json.dump()和json.load()

    import json #导入json模块
    numbers = [2,3,5,7,11,13]
    
    filename = 'number.json' #存储目标文件名称,数据为json格式
    with open(filename,'w') as f: #写入模式打开这个文件,让json把数据写入
        json.dump(numbers,f) #使用json.dump()存储文件
    
    import json
    
    filename = 'numbers.json'
    with open(filename) as f:
        numbers = json.load(f) #加载数据
    print(numbers)
    

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

    import json
    #如果存储过用户名就加载它
    #否则,提示用户输入用户名并存储它
    
    filename = 'username.json'
    try:
        with open(filename) as f:
            username = json.load(f)
    except FileNotFoundError:
        username = imput("what is your name?")
        with open(filenme,'w') as f:
            json.dump(username,f)
            print("we'll remember you when you come back, " + username +"!")
    else:
        print("welcome back, " + username + "!")
    

    10.4.3 重构

    将代码划分为一系列完成具体工作的函数,让代码更清晰、更易于理解、更容易扩展

    import json
    def get_sorted_username():
        """如果存储了用户名就获取它"""
        filename = "username.json"
        try:
            with open(filename) as f:
                username = json.load(f)
        except FileNotFoundError:
            return None
        else:
            return username
    
    def get_new_username():
        """提示输入用户名"""
        username = input("what is your name?")
        filename = "username.json"
        with open(filename,'w') as f:
            json.dump(username,f)
        return username
        
    def greet_user():
        """问候用户,并指出名字"""
        username = get_sorted_username()
        if username:
            print("welcome back, "  + username + "!")
        else:
            username = get_new_username()
            print("we'll emember you when you come back, "+username +"!")
    
    greet_user()
    

    相关文章

      网友评论

          本文标题:【Python】10 文件和异常

          本文链接:https://www.haomeiwen.com/subject/fpmhectx.html