zoukankan      html  css  js  c++  java
  • python学习之文件基本操作

    1、文件与文件模式介绍

    1.1 什么是文件

    文件是操作系统提供给用户/应用程序操作硬盘的一种虚拟的概念/接口

    一个完整的计算机系统包括3层结构:

    ​ 用户/应用程序(open())
    ​ 操作系统(文件)
    ​ 计算机硬件(硬盘)

    1.2 为什么要用文件

    用户/应用程序可以通过文件将数据永久保存在硬盘中
    即操作文件就是操作硬盘

    用户/应用程序直接操作的是文件,对文件进行的所有的操作,都是
    在向操作系统发送系统调用,然后再由操作将其转换成具体的硬盘操作

    1.3 如何用文件:open()

    ​ 控制文件读写内容的模式:t和b
    ​ 强调:t和b不能单独使用,必须跟r/w/a连用

    ​ t文本(默认的模式)
    ​ 1、读写都以str(unicode)为单位的
    ​ 2、文本文件
    ​ 3、必须指定encoding='utf-8'

    ​ b二进制/bytes

    ​ 控制文件读写操作的模式
    ​ r只读模式
    ​ w只写模式
    ​ a只追加写模式
    ​ +:r+、w+、a+

    2、文件的基本操作

    1)打开文件

    windows路径分隔符问题

    open('C:a.txt
    bcd.txt')
    

    解决方案一:推荐

    open(r'C:a.txt
    bcd.txt')
    

    解决方案二:

    open('C:/a.txt/nb/c/d.txt')
    

    2)操作文件

    操作文件即读/写文件,应用程序对文件的读写请求都是在向操作系统发送
    系统调用,然后由操作系统控制硬盘把输入读入内存、或者写入硬盘

    res=f.read()
    print(type(res))
    print(res)
    

    3)关闭文件

    f.close() # 回收操作系统资源
    print(f)
    f.read() # 变量f存在,但是不能再读了
    
    del f     # 回收应用程序资源
    

    3、with上下文管理

    文件对象又称为文件句柄

    with open('a.txt',mode='rt') as f1: # f1=open('a.txt',mode='rt')
        res=f1.read()
        print(res)
    
    with open('a.txt',mode='rt') as f1,
            open('b.txt',mode='rt') as f2:
        res1=f1.read()
        res2=f2.read()
        print(res1)
        print(res2)
    

    4、指定字符编码

    强调:t和b不能单独使用,必须跟r/w/a连用

    t文本(默认的模式)
    1)读写都以str(unicode)为单位的
    2)文本文件
    3)必须指定encoding='utf-8'

    没有指定encoding参数操作系统会使用自己默认的编码
    linux系统默认utf-8
    windows系统默认gbk

    with open('c.txt',mode='rt',encoding='utf-8') as f:
        res=f.read() # t模式会将f.read()读出的结果解码成unicode
        print(res,type(res))
    

    内存:utf-8格式的二进制-----解码-----》unicode
    硬盘(c.txt内容:utf-8格式的二进制)

    5、文件操作模式详解

    以t模式为基础进行内存操作

    5.1 r(默认的操作模式):只读模式,当文件不存在时报错,当文件存在时文件指针跳到开始位置

    with open('c.txt',mode='rt',encoding='utf-8') as f:
        print('第一次读'.center(50,'*'))
        res=f.read() # 把所有内容从硬盘读入内存
        print(res)
    
    # with open('c.txt', mode='rt', encoding='utf-8') as f:
    
    print('第二次读'.center(50,'*'))
    res1=f.read()
    print(res1)
    

    =案例====

    inp_username=input('your name>>: ').strip()
    inp_password=input('your password>>: ').strip()
    
    # 验证
    with open('user.txt',mode='rt',encoding='utf-8') as f:
        for line in f:
            # print(line,end='') # egon:123
    
            username,password=line.strip().split(':')
            if inp_username == username and inp_password == password:
                print('login successfull')
                break
        else:
            print('账号或密码错误')
    

    应用程序》文件
    应用程序
    》数据库管理软件=====》文件

    5.2 w:只写模式,当文件不存在时会创建空文件,当文件存在会清空文件,指针位于开始位置

    with open('d.txt',mode='wt',encoding='utf-8') as f:
        f.read() # 报错,不可读
        f.write('擦勒
    ')
    

    强调1:
    在以w模式打开文件没有关闭的情况下,连续写入,新的内容总是跟在旧的之后

    with open('d.txt',mode='wt',encoding='utf-8') as f:
        f.write('擦勒1
    ')
        f.write('擦勒2
    ')
        f.write('擦勒3
    ')
    

    强调2:
    如果重新以w模式打开文件,则会清空文件内容

    with open('d.txt',mode='wt',encoding='utf-8') as f:
        f.write('擦勒1
    ')
    with open('d.txt',mode='wt',encoding='utf-8') as f:
        f.write('擦勒2
    ')
    with open('d.txt',mode='wt',encoding='utf-8') as f:
        f.write('擦勒3
    ')
    

    案例:w模式用来创建全新的文件
    文件文件的copy工具

    src_file=input('源文件路径>>: ').strip()
    dst_file=input('源文件路径>>: ').strip()
    with open(r'{}'.format(src_file),mode='rt',encoding='utf-8') as f1,
        open(r'{}'.format(dst_file),mode='wt',encoding='utf-8') as f2:
        res=f1.read()
        f2.write(res)
    

    5.3 a:只追加写,在文件不存在时会创建空文档,在文件存在时文件指针会直接调到末尾

    with open('e.txt',mode='at',encoding='utf-8') as f:
        # f.read() # 报错,不能读
        f.write('擦嘞1
    ')
        f.write('擦嘞2
    ')
        f.write('擦嘞3
    ')
    

    强调 w 模式与 a 模式的异同:
    1 相同点:在打开的文件不关闭的情况下,连续的写入,新写的内容总会跟在前写的内容之后
    2 不同点:以 a 模式重新打开文件,不会清空原文件内容,会将文件指针直接移动到文件末尾,新写的内容永远写在最后

    案例:a模式用来在原有的文件内存的基础之上写入新的内容,比如记录日志、注册
    注册功能

    name=input('your name>>: ')
    pwd=input('your name>>: ')
    with open('db.txt',mode='at',encoding='utf-8') as f:
        f.write('{}:{}
    '.format(name,pwd))
    

    了解:+不能单独使用,必须配合r、w、a

    with open('g.txt',mode='rt+',encoding='utf-8') as f:
        # print(f.read())
        f.write('中国')
    
    with open('g.txt',mode='w+t',encoding='utf-8') as f:
        f.write('111
    ')
        f.write('222
    ')
        f.write('333
    ')
        print('====>',f.read())
        
    with open('g.txt',mode='a+t',encoding='utf-8') as f:
        print(f.read())
    
    f.write('444
    ')
    f.write('5555
    ')
    print(f.read())
    
  • 相关阅读:
    Oracle学习(四)--sql及sql分类讲解
    Oracle学习(三)--数据类型及常用sql语句
    Oracle学习(二)--启动与关闭
    Tomcat学习笔记--启动成功访问报404错误
    有关Transaction not successfully started问题解决办法
    百度富文本编辑器UEditor1.3上传图片附件等
    hibernate+junit测试实体类生成数据库表
    js登录与注册验证
    SVN安装配置与使用
    [LeetCode] #38 Combination Sum
  • 原文地址:https://www.cnblogs.com/leilijian/p/12489005.html
Copyright © 2011-2022 走看看