• 文件操作模式详解


    一、with的使用

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

    with方法可以在执行完子代码后自动关闭文件,节约内存空间。

    二、指定字符编码

    强调: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))

    三、文件操作模式详解

    1584090160171[3]

    模式mode 操作 若不存在 是否清空
    r 只能读不能写 报错 -
    rb 二进制只读 报错 -
    r+ 可读可写 报错
    rb+ 二进制读写 报错
    w 只能写不能读 创建文件
    wb 二进制只写 创建文件
    w+ 可读可写 创建文件
    wb+ 二进制读写 创建文件
    a 追加不能读 创建文件 否,追加写
    ab 二进制追加不能读 创建文件 否,追加写
    a+ 可读可写 创建文件 否,追加写
    ab+ 二进制追加可读可写 创建文件 否,追加写



    只读模式

    下面以文本模式为例

    # 1、r(默认的操作模式):只读模式,当文件不存在时报错,当文件存在时文件指针跳到开始位置
    with open('c.txt',mode='rt',encoding='utf-8') as f:
        res=f.read() # 把所有内容从硬盘读入内存
       print(res)
    # 一次性读完,若文件较大会比较吃力

    案例:使用存在硬盘内的账户信息登录验证

    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
            # 程序会循环多次若找到了对应的账号密码会退出,若循环结束了还没找到,则登录失败。
           username,password=line.strip().split(':')
            if inp_username == username and inp_password == password:
                print('login successfull')
                break
        else:
            print('账号或密码错误')

    只写模式

    当文件不存在时会创建空文件,当文件存在会清空文件,指针位于开始位置
    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)
    # 1.用户分别输入文件1和文件2的路径
    # 2.先以只读的方式打开文件1,同时以只写的方式打开文件2,将文件1中的数据读出并赋值给f1,再写# 入到文件2中。

    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 模式的异同: 相同点:在打开的文件不关闭的情况下,连续的写入,新写的内容总会跟在前写的内容之后。 不同点:以 a 模式重新打开文件,不会清空原文件内容,会将文件指针直接移动到文件末尾,新写的内容永远写在最后。

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

    注册功能

    name=input('请输入用户名:')
    pwd=input('请输入密码:')
    with open('db.txt',mode='at',encoding='utf-8') as f:
        f.write('{}:{} '.format(name,pwd))

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

    # r+
    with open('g.txt',mode='rt+',encoding='utf-8') as f:
        # print(f.read())
        f.write('中国')
    # w+
    with open('g.txt',mode='w+t',encoding='utf-8') as f:
        f.write('111 ')
        f.write('222 ')
        f.write('333 ')
        print('====>',f.read())
    # a+
    with open('g.txt',mode='a+t',encoding='utf-8') as f:
        print(f.read())

        f.write('444 ')
        f.write('5555 ')
        print(f.read())

    1584093852503[3]


  • 相关阅读:
    十、collection的作用+变量
    python借助ADB工具实现自动化操作手机
    selenium 对浏览器标签页进行关闭和切换
    python编写shell脚本
    报错解决——# Creating Server TCP listening socket *:6379: bind: Address already in use
    Mac 报错:-bash: telnet: command not found
    Mac快捷键大全及cheatsheet插件
    报错解决——xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
    Mac破解免费office软件
    python 当前时间多加一天、一小时、一分钟
  • 原文地址:https://www.cnblogs.com/bailongcaptain/p/12488074.html
Copyright © 2020-2023  润新知