什么是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))