Python之路--文件操作

内容概要

文件读写模式
文件的其他操作

文件的读写模式

f = open('文件名',mode = '',encoding = '')
文件名必须是字符串
mode 是模式,不同的模式,可以达到不同的效果
r 只读模式
rb 读取字节编码
w 只写模式
wb 修改字节编码
a 追加模式
ab 追加字节编码
r+ 读写模式
w+写读模式
a+写读模式

只读

f = open('文件名',mode = 'r',encoding = 'utf-8')
或者
with open('文件名',mode = 'r',encoding = 'utf-8') as f:
[上下文管理]:会自动关闭文件

#t2.py文件内容:第一次测试
f = open('t2.py','r',encoding = 'utf-8')
print(f.read()) #这里的mode= 可以省略,直接写模式即可
#输出结果:第一次测试
注意:文件名的位置需要制定文件的路径+文件名
可使用相对路径和绝对路径,使用方法如下:
第一种:绝对路径引用

f = open('C:\Users\15471\Desktop\123.tet','r',encoding = 'utf-8')
#这种文件位置引用会报错,正确修改方法
f = open('C:\\Users\\15471\Desktop\\123.txt','r',encoding = 'gbk')
#或者
f = open(r'C:\Users\15471\Desktop\123.txt','r',encoding = 'gbk')
print(f.read())
第二种 相对路径引用

f = open('t2.py','r',encoding = 'utf-8')
print(f.read())  #同一文件夹下
f = open(r'..\day06\1234.py',mode = 'r',encoding = 'utf-8')   #不同文件夹下(..\代表上一层文件,再加一个代表再往上一层)
print(f.readline())

文件内容读取

f.read() 若括号里为空,代表全部一次性读取,若有数字则代表读取前多少个字符

#t2内容('第一次测试')
f = open('t2.py','r',encoding = 'utf-8')
print(f.read(2)) #只读前两个字符
打印内容:第一
f.readline()读取第一行,若里面有数字,按照数字前多少个提取,但是如果字数大于本行的总字数,还是返回本行的内容.

f = open('t2.py','r',encoding = 'utf-8')
print(f.readline())
打印内容:第一次测试
f = open('t2.py','r',encoding = 'utf-8')
print(f.readline(2))
打印内容:第一
f = open('t2.py','r',encoding = 'utf-8')
print(f.readline(6))
打印内容:第一次测试
f.reanlines() 以列表的形式呈现所有的内容,若括号里有数字(大于0),如果数字小于本行的长度,则返回本行,如果大于本行小于下一行的长度,则返回两行,以此类推;结果以列表的形式呈现.

#t2内容(
第一次测试
又新增一行
)
f = open('t2.py','r',encoding = 'utf-8')
print(f.readlines())
打印内容:['第一次测试\n', '又新增一行']

读取字节码

f = open('文件名',mode = 'rb')
or
with open('文件名',mode = 'rb') as f:

with open('t2.py','rb')as f:
    print(f.read())
打印结果:b'\xe7\xac\xac\xe4\xb8\x80\xe6\xac\xa1\xe6\xb5\x8b\xe8\xaf\x95\r\n\xe5\x8f\x88\xe6\x96\xb0\xe5\xa2\x9e\xe4\xb8\x80\xe8\xa1\x8c'

只写

写入文件内容

with open('文件名',mode = 'w',encoding = 'utf-8') as f:
with open('t2.py','w',encoding='utf-8')as f:
    print(f.write("今天天气真好"))
打印结果:6
t2文件内容:今天天气真好

注意:write在进行写入的过程中,是先把原有内容全部清除再进行写入,打印的内容是写入的字符长度

with open('t5.py','w',encoding='utf-8')as f:
    print(f.write("今天天气真好"))
注意:若需要写入的文件不存在,它会先新建一个,再写入

写入字节码

with open('文件名',mode = 'wb') as f:

f = open('timg.jpg','rb')
f1 = open('timg.jpg','wb')
f1.write(f.read())
原理:先打开要修改的文件,转成字节码后,再进行写入,相当于复制一份
如果要单独写入字节码,就不需要打开f文件

追加模式

追加文件内容

with open('文件名',mode = 'r',encoding = 'utf-8') as f:
#t2内容('第一次测试')
with open('t2.py','a',encoding='utf-8')as f:
    print(f.write("追加记录"))
打印结果:4
t2文件内容:今天天气真好追加记录 #默认在文件末尾添加

 追加字节码

with open('文件名',mode = 'ab') as f:
跟上文一样,在里面就不赘述

可读可写模式

with open('文件名',mode = 'r+',encoding = 'utf-8') as f:

with open('t2.py','r+',encoding='utf-8')as f:
    print(f.read())
    print(f.write('尝试一下'))
t2文件内容:今天天气真好尝试一下 默认在文件末尾添加

with open('t2.py', 'r+', encoding='utf-8')as f:
    print(f.write('尝试一下'))
    print(f.read())
t2文件内容:尝试一下真好 #把之前的'今天天气给覆盖了'
注:尽量采用先读后写模式,如果先写后读会默认从开头写入,造成文件内容混乱

可写可读模式

with open('文件名',mode = 'w+',encoding = 'utf-8') as f:

with open('t2.py','w+',encoding='utf-8')as f:
    f.write("再试试,看可不可以")
    f.seek(0)
    print(f.read())
运行结果:再试试,看可不可以
因为write是先删除后写入,所以之前的内容就没有了
with open('t2.py','w+',encoding='utf-8')as f:
    f.seek(0) #定位光标的位置,里面的数字代表字节长度
    print(f.read())
    f.write("再试试,看可不可以")
注:尽量采用先写后读,因为先读后写,后面写的内容是无法读取的
with open('文件名',mode = 'a+',encoding = 'utf-8') as f:

with open('t2.py','a+',encoding='utf-8')as f:
    f.write('第三种方法再试试')
    f.seek(0)
    print(f.read())
运行结果:今天天气真好第三种方法再试试
with open('t2.py','a+',encoding='utf-8')as f:
    f.seek(0)
    print(f.read())
    f.write('第三种方法再试试')
运行结果:今天天气真好
注:尽量采用先写后读,因为先读后写,后面写的内容无法读取的

文件的其他操作

with open('t2.py','a+',encoding='utf-8')as f:
    print(f.seek(3))#定位光标的位置,一个字节是一个
print(f.tell())
    #显示光标所在位置之前的字节长度
    print(f.read())
#打印结果:3 3 今天天气真好

seek用法

单数字
代表字节所在的位置,3个字节是一个汉字,1个字节是一个英文
seek(0,0) 起始位置
seek(0,1)当前位置
seek(0,2)末尾位置

f.truncate() 截取

根据括号的数字定位字节位置,删除字节后面的数据,3个字节是1个中文字符
##今天天气真好
with open('t2.py','a+',encoding='utf-8')as f:
    f.truncate(3)
运行结果:今

文件的重命名

import os
with open('t2.py','r',encoding='utf-8') as f, \
    open('t3.py',mode = 'a',encoding='utf-8') as f1:
        msg = f.read()
        msg = msg.replace('','')
        f1.write(msg)
os.remove('t3.py')
os.rename('t3','t2')
第二种方法--逐条执行,减轻电脑压力

import os
with open('t1.py','r',encoding='utf-8') as f, \
    open('t5.py',mode = 'a',encoding='utf-8') as f1:
    for i in f:
        i = i.strip().replace('','')
        f1.write(i)
os.remove('t1.py')
os.rename('t5.py','t1.py')

猜你喜欢

转载自www.cnblogs.com/Ailsa-a/p/10305945.html
今日推荐