文件操作

文件操作

将内存中的数据持久化存储到文件中

【一】打开文件的两种方式

【1】使用open语句

#三个参数
# 文件路径、
# 打开文件的模式(w覆盖写、a追加写、r读)、
# 编码格式
1.打开文件并赋给句柄对象
fp = open('01.txt','r',encoding='utf-8')
print(fp)

<_io.TextIOWrapper name='01.txt' mode='r' encoding='utf-8'>

2.读取所有文件数据
data = fp.read()

3.打印
print(data)

666!

4.关闭
fp.close()

【2】with语句

  • 在with语句内部打开文件以后自动执行 close() 把文件关闭--- 节省资源
with open('01.txt','r',encoding='utf8') as fp:
    data = fp.read()
    print(data)
    
	666!

【二】文件的操作模式

【1】读模式 r

with open('01.txt', 'r', encoding='utf8') as fp:
    data = fp.read()
username = data.split('|')
print(username)

['666!']

#字符串格式化语法
# f:格式化输出
# b:将字符串转为二进制
# r:取消转义

print('a\nb')
print('a\\nb')
print(r'a\nb')

a
b
a\nb
a\nb

print('D:\pythonProject\08\01.txt')
print(r'D:\pythonProject\08\01.txt')

D:\pythonProject 8.txt #两处乱码
D:\pythonProject\08\01.txt

【2】覆盖写模式 w

1.一次性写入
# 打开当前文件并将文件内容清空并写入新内容
# 文件存在则打开,不存在则新建
data = 'opp|123'
with open('01.txt','w',encoding='utf-8') as fp:
    fp.write(data)
文件内容原为666! 覆盖后为opp|123   
2.连续写入
data = 'opp|123'
with open('01.txt','w',encoding='utf-8') as fp:
    fp.write(data)
    fp.write('asdasdsasd')
    
文件内容原为opp|123  覆盖后为 opp|123asdasdsasd

# 关闭文件后无法写入
	fp.close() # 关闭
    # 继续写入
    fp.write('9999')
    #报错
   	# I/O operation on closed file.

【3】追加写模式 a

# 文件存在则打开并写入,不存在则新建后写入
# 打开当前文件并在最后拼接写入新内容
# 不自带换行功能
data = 'opp|123'
with open('01.txt','a',encoding='utf-8') as fp:
    fp.write(data)
    fp.write('asdasdsasd')
文件内容原为opp|123asdasdsasd 追加写后变为:
opp|123asdasdsasdopp|123asdasdsasd

# 带上换行
data = 'opp|123'
with open('01.txt','a',encoding='utf-8') as fp:
    fp.write('\n'+ data + '\n')
    fp.write('asdasdsasd')
# 文件内容变为
opp|123asdasdsasdopp|123asdasdsasd
opp|123
asdasdsasd

【4】文件操作模式补充

# 【1】文本编辑模式
w / a / r
# 完整的模式应该是 
wt / at / rt
# 意思是大文本编辑模式
# 【2】二进制数据模式
wb / rb # 只能读写二进制
# 将图片转为二进制数据
with open('img.png', 'rb') as fp:
    data = fp.read()
# 将二进制数据转为图片   
with open('girl.jpg', 'wb') as fp:
    fp.write(data)
 

【5】文件操作扩展模式

r+(先有文件才能读写)/ w+ /a+
# r :只读无法写
# w:只写无法读
# 为了能又读又写
with open('01.txt', 'r+', encoding='utf-8') as fp:
    fp.write('666')
    # 写完以后需要进行持久化保存才能被读出来,否则是空的
    data = fp.read()
print(data)

文件中:666|123asdasdsasdopp|123asdasdsasd

输出:|123asdasdsasdopp|123asdasdsasd

没有读出来666

【6】文件的操作方法详细

使用追加写模式做数据准备

data = 'qwer|123'
with open('01.text','a',encoding='utf8') as fp:
    fp.write(data + '\n')

文件中:
qwer|123
qwer|123
qwer|123
qwer|123
qwer|123

【1】 r模式的方法详细

read()和readline()会将文件中的所有内容作为一个字符串返回

readlines()则是返回一个包含文件所有行的列表

# (1)一次性全读完
with open('01.text','r',encoding='utf8') as fp:
    data = fp.read()
    print(data)

qwer|123
qwer|123
qwer|123
qwer|123
qwer|123
# (2)每次只读一条
with open('01.text','r',encoding='utf8') as fp:
    data = fp.readline()
    print(data)
    
qwer|123

输出全部
with open('01.text','r',encoding='utf8') as fp:
    count = 0
    while True:
        count += 1
        data = fp.readline()
        if data:
            print(count, data)
        else:
            break
            
1 qwer|123

2 qwer|123

3 qwer|123

4 qwer|123        

5 qwer|123
# (3)一次读很多行 readlines()
# 读出来是个列表
with open('01.text','r',encoding='utf8') as fp:
    data = fp.readlines()
    print(data)
    
['qwer|123\n', 'qwer|123\n', 'qwer|123\n', 'qwer|123\n', 'qwer|123\n']
# (4)判断当前对象是否可读
# print(fp.readable())
with open('01.text','r',encoding='utf8') as fp:
    fp.close()
    print(fp.readable())
    报错
    不关闭文件时 True
# read补充
文件:1234567
with open('01.text', 'r', encoding='utf8') as fp:
    # read 可以放参数,参数是读取到哪个索引位置
    data = fp.read(4)
print(data)

1234

【2】 w写模式的方法详细

 with open('01.text','w',encoding='utf8') as fp:
# (1)将文件所有内容清空并一次性写入
    fp.write('adad')
#(2)逐个元素进行写入 写入的内容必须为可迭代类型
# fp.readlines()
    data_list = ['1','\n','2','\n','333']
    fp.writelines(data_list)

写入后:
1
2
333

【3】a追加写模式的方法详细

同w写模式

 with open('01.text','a',encoding='utf8') as fp:
# (1)将文件所有内容清空并一次性写入
    fp.write('adad')

1
2
333adad

#(2)逐个元素进行写入 写入的内容必须为可迭代类型
# fp.readlines()
    data_list = ['1','\n','2','\n','333']
    fp.writelines(data_list)

写入后:
1
2
333adadadad1
2
333

【三】控制文件指针

seek函数

# f.seek(指针移动的字节数,模式控制):
# 0模式: 默认的模式,该模式代表指针移动的字节数是以文件开头为参照的
# 1模式: 该模式代表指针移动的字节数是以当前所在的位置为参照的
# 2模式: 该模式代表指针移动的字节数是以文件末尾的位置为参照的
0模式 以文件开头为参照的
文件:你好!你好,你好。你好/你好?
with open('01.text', 'r', encoding='utf8') as fp:
    # 以开头作为参照,向后移动一个字符
    # 英文字符占一个字符位置,中文字符占三个字符位置
    # 从0开始,往右移动三个字符
    fp.seek(3,0)
    # 当前索引位置
    print(fp.tell())
    print(fp.read())
    
好!你好,你好。你好/你好?
	# 0:表示0模式,以开头为参照,用一次重置一次
    # 两次输出相同
	fp.seek(3,0)
    print(fp.tell())
    print(fp.read())
好!你好,你好。你好/你好?    
1模式 以当前位置作为参照向后移动
在Python3.x版本之后不允许在文本文件中使用
只能在二进制中使用
print(fp.read())   
fp.seek(3, 1) 
2模式 以文件末尾的位置为参照移动
# fp.seek(-2, 2)  # \x82'
    # print(fp.tell())
    # print(fp.read())
posted @   随机昵称yi  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示