python第五部分:文件操作

什么是IO?IO流介绍

1.什么是IO

input:输入/读取

output:输出/写出

以程序为参照物,写出的过程就是:从程序到本地/外界,读入的过程就是:从本地/外界到程序

python中简化了IO的操作

2.IO的分类

分类一:

输入流

输出流

分类二:

字符流

字节流

3.open函数

使用:open(filepath文件路径,mode读写模式,encoding字符集,缓冲区)

复制代码
# 1.测试open函数
file1 = open("./code/05python_io/testIO.txt")
## <_io.TextIOWrapper name='./code/05python_io/testIO.txt' mode='r' encoding='cp65001'>

print(file1)

# 2.测试文件变量的操作
print(dir(file1))

4.操作文件本身

name文件名

mode读写模式

复制代码
print("文件的名称:",file1.name)
print("文件的读写模式",file1.mode) # r

print("是否可读",file1.readable()) # T
print("是否可写",file1.writable()) # F

print("判断流是否关闭:",file1.closed) # F
file1.close() # 关闭流 --释放资源
print("判断流是否关闭:",file1.closed) # T

IO怎么用?文件操作

1.文件内容的读写

写:

文件如果不存在,则默认创建

复制代码
# 1.测试文件的写入
file1 = open("./code/05python_io/testIO.txt","w",encoding="utf-8")
res = file1.write("zhangsan")
print(res) # 返回 当前 写入字符的长度
print(type(res)) # <class 'int'>

res = file1.write("lisi") # 用同一对象时,写是不覆盖的,是追加
print(res) # 返回 当前 写入字符的长度

读:

文件如果不存在,则会报错

复制代码
# 2.测试文件的读取
file2 = open("./code/05python_io/testIO.txt","r",encoding="utf-8")
# 2.1 读取部分数据
## 读取4个字符,指针会向后移动4位
res = file2.read(4) # zhan
print(res)

# 2.2 读取所有数据 
## 如果之前读取了部分数据,则读取的是剩余部分
res = file2.read() #gsanlisi
print(res)

# 2.3 行读取
## 每次读取一行
file3 = open("./code/05python_io/test_read.txt","r",encoding="utf-8")

res = file3.readline()
print(res)

# 2.4 行读取所有
## 返回一个列表
res2 = file3.readlines()
print(res2)

# 5.课堂练习:任意选择一个文件的内容复制到该文件
file_copy_read = open("./code/05python_io/test_seek.txt","r",encoding="utf-8")
file_copy_write = open("./code/05python_io/test_seek_copy.txt","r",encoding="utf-8")
file_copy_write.write(file_copy_read.read())
file_copy_read.close()

2.文件的缓冲区

缓冲区减少了程序和文件的交互次数

缓冲区默认容量:8192或4196

buffering = -1 默认值 无缓冲

1:行缓存 碰见换行符时刷新缓冲区

x>1:全缓冲

复制代码
# 3. 测试缓冲区
file_buffer = open("./code/05python_io/test_buffer.txt","w",encoding="utf-8",buffering=10)
file_buffer.write("111")

import os
print("test_buffer.txt的长度为:",os.path.getsize("./code/05python_io/test_buffer.txt")) # 0

file_buffer.flush() # 刷新缓冲区,一般使用在需主动写入文件时,清空缓冲区的内容
file_buffer.close() # 关闭流时会清空缓冲区

print("test_buffer.txt的长度为:",os.path.getsize("./code/05python_io/test_buffer.txt")) # 3

3.文件指针操作

seek

teil

复制代码
# 4.测试指针问题
file_seek = open("./code/05python_io/test_seek.txt","r",encoding="utf-8")
# 移动指针的位置
file_seek.seek(8) # 将文件指针移动到第8个字节的位置
print(file_seek.read())
print(file_seek.tell()) # 23

4.扩展:文件的读写模式

w:只写

会自动创建

第二次创建流文件时,会覆盖

r:只读

如果文件不存在,则会报错

复制代码
# 1.测试基本读写和追加问题
f1 = open("code\\05python_io\\test1.txt","w",encoding="utf-8")
f1.write("zhangsan\n")
f1.write("zhangsan2")
f1.read() # not readable
f1.close()

# 只写模式,如果多次创建流写入内容时会覆盖
f1 = open("code\\05python_io\\test1.txt","w",encoding="utf-8")
f1.write("zhan\n")
f1.write("zhan2")
f1.close()

a:追加

会自动创建

可写但不可读取

w+:可读可写 可自动创建文件

会清空原有文件内容并进行读写

r+:可读可写 不可自动创建文件

指针位置在首位,不会清空源文件内容

a+:可读可写

指针位置会变化到末尾,因此读不到前面的内容

wb,rb,ab 二进制文件的读写

复制代码
# 3.测试二进制的问题
f4 = open("code\\05python_io\\test2.txt","wb")
f4.write(b"\b00\b01\b02")
f4.close()

f4 = open("code\\05python_io\\test2.txt","rb")
print(f4.read())
f4.close()

IO的优化?with语法

python独有

1.with语法的使用

简化流操作

会默认关闭流

格式:

with open函数 as 别名:

复制代码
# 1.测试with语法
with open("code\\05python_io\\test2.txt","w",encoding="utf-8") as f:
    f.write("zhangsan")

2.with语法多问加操作

复制代码
# 2.文件复制案例
with open("code\\05python_io\\test2_copy.txt","w",encoding="utf-8") as f1:
    with open("code\\05python_io\\test2.txt","r",encoding="utf-8") as f2:
        f1.write(f2.read())
        
# 3.with语法多文件操作
file_name = "code\\05python_io\\test2.txt"
file_copy_name = "code\\05python_io\\test2_copy.txt"
with open(file_copy_name,"w",encoding="utf-8") as f1,open(file_name,"r",encoding="utf-8") as f2:
    f1.write(f2.read())

print(f1.closed) # T 自动关闭流
print(f2.closed) # T

IO的应用?序列化

1.序列化和反序列化

序列化:

将对象(列表/字典/函数/变量)转为字节序列

例子:为了将一个函数存储到本地,将它变成字节或字符序列

反序列化:

将字节序列转化为对象

2.dumps和loads函数

复制代码
import pickle

# 一、测试序列化
# 1.创建列表
list1 = [10,20,30,"zhangsan","lijian"]
# 2.序列化 
res = pickle.dumps(list1)
print(res)
print(type(res)) # <class 'bytes'>

# 3.将二进制字节序列写入文件中
with open("code\\05python_io\\test2_copy.txt","wb") as f:
    f.write(res)

# 二、测试反序列化
with open("code\\05python_io\\test2_copy.txt","rb") as f:
    res = f.read()
## 反序列化
list_res = pickle.loads(res)
print(list_res)

3.dump和load函数

复制代码
# 三、测试序列化和反序列化的读写
def sum1 (x,y):
    return x+y

## 序列化并存储
pickle.dump(sum1,open("code\\05python_io\\test2_copy.txt","wb"))

## 反序列化并读取
res = pickle.load(open("code\\05python_io\\test2_copy.txt","rb"))
print(res(10,20))
相关推荐
鸽芷咕1 小时前
无需额外运维!金仓KES V9一站式承接MongoDB全场景需求
运维·数据库·mongodb
晚霞的不甘2 小时前
Flutter for OpenHarmony《智慧字典》英语学习模块代码深度解析:从数据模型到交互体验
前端·学习·flutter·搜索引擎·前端框架·交互
三水不滴2 小时前
从原理、场景、解决方案深度分析Redis分布式Session
数据库·经验分享·redis·笔记·分布式·后端·性能优化
踢足球09292 小时前
寒假打卡:1-29
数据库
wniuniu_2 小时前
日志内容和cephadm
数据库·ceph
多打代码2 小时前
2026.1.29 复原ip地址 & 子集 & 子集2
开发语言·python
Nandeska2 小时前
11、MySQL主从复制的基本概念
数据库·mysql
人工智能AI技术2 小时前
【Agent从入门到实践】47 与前端系统集成:通过API对接,实现前端交互
人工智能·python
蓝黑20202 小时前
SQL的union和union all
数据库·sql