文件处理详解

一、with上下文管理

一、

文件对象又称之为文件句柄,也就是用来操作文件的

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

二、

当需要用with打开两个一下打开两个 或者多个文件时用(,\)隔开

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)
    # f1.close()
    # f2.close()

with子代码运行完后,文件会自动关闭,所以后两步可以不写

二、指定字符编码

一、t文本读写模式(默认)

1、读写都以str(unicode)为单位的

2、文本文件
3、必须指定encoding='utf-8'

二、encoding指定编码

如果没有指定encoding的话,操作系统使用自己的编码方式

1.linuxs系统默认的解码方式是uft-8

2.windows系统默认的是gbk

3.指定encoding

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

三、文件操作模式详解

以下都是在t模式为基础进行内存操作

一、r(默认操作模式),只读模式

1、r模式特点

在文件不存在的时候会报错,文件存在,打开文件时,文件指针会跳到开始位置

2、r模式操作的基本方法

with open('c.txt', mode='rt', encoding='utf-8') as f:
    res1=f.read() #刚打开文件的时候文件指针在起始位置,从头开始读
    print(res1)

3、r模式操作文件的使用案例

可以用于检测用户的登录信息是否跟文件存的信息相匹配,验证登录

用户登录验证:

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('账号或密码错误')

4、强调

读取的文件不存在时会报错,只读模式不能进行写操作

二、只写模式

1、w模式特点

当文件不存在时会创建空的新文件,而当文件存在的时候,如果文件部位空,会清空文件,并且每次用

w模式打开文件,指针都位于开始位置

2、w模式操作的基本方法

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

3、w模式操作文件的使用案例

(一般用于创建全新的文件,重要的有数据的文件不要用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)

4、强调

1、在以w模式打开文件没有关闭的情况下,连续写入,新的内容总是跟在旧的之后,不会清空之前的写入

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

2、每次重新打开文件的时候,就会对之前的写人的文件进行情况,保留最后一次的写入

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

3、w只读模式,不能进行读f.read()操作,否则会报错

三、a只追加写模式

1、a模式特点

和w相同的是,在文件不存在的时候会创建新的空的文件,而在文件存在的,打开文件时, 文件指针会跳到

末尾,在末尾进行写操作,不会改变之前的写入,所以叫做追加写

2、a模式操作的基本方法

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

3、a模式操作文件的使用案例

(用于不改变文件里原有的数据的前提下,加入新的数据)

注册新用户:

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

4、强调

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

四、了解

(+加号不能单用,必须配合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\n')
    f.write('222\n')
    f.write('333\n')
    print('====>',f.read())


with open('g.txt',mode='a+t',encoding='utf-8') as f:
    print(f.read())

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

声明:内容、思想抄自egon老湿,如有雷同.....那就对了—》

https://zhuanlan.zhihu.com/p/108808704

posted @ 2020-03-13 23:17  风起千寻  阅读(208)  评论(0编辑  收藏  举报