文件
- 长久保存信息的一种数据信息集合(持久化)
- 常用操作
- 打开关闭(文件一旦打开,需要关闭操作)
- 读写内容
- 查找
open 函数
- open函数负责打开文件,带有很多参数
- 第一个参数:必须有,文件的路径和名称
- mode:表明文件用什么方式打开(读,写)
- r:以只读方式打开
- w:写方式打开,会覆盖以前的内容
- x:创建方式打开,如文件已经存储,报错
- a:append方式,以追加的方式对文件内容进行写入
- b:binary方式,二进制方式写入
- t:文本方式打开
- +:可读写
- 注:若不指定文件路径,默认在本路径下创建文件
f = open(r"test01.txt",'w')
f.close()
with语句
- with语句使用得技术是一种成为上下文管理协议得技术(ContextMangagementProtoal),由系统负责关闭文件。
- 自动判断文件的作用域,自动关闭不在使用的打开的文件句柄
- 对文件的使用一般要求使用with打开文件
with open(r"test01.txt",'r') as f:
pass
readline
with open(r"file01.txt",'r') as f:
strline = f.readline()
while strline:
print(strline)
strline = f.readline()
list
- 整个文件读取,把文件内每一行作为一个元素,进行遍历
with open(r"file01.txt", 'r') as f:
l = list(f)
for line in l:
print(line)
read
- 是按照字符读取文件内容
- 允许输入参数决定读取几个字符,如果没有制定,从当前位置读取到结尾
- 否则,从当前位置(文字指针位置)读取指定个数字符
with open(r'file01.txt', 'r' ) as f:
strChar = f.read()
print(len(strChar))
print(strChar)
seek(offset,from)
- 移动文件的读取位置,也叫读取指针
- from的取值范围:
- 0: 从文件头开始偏移
- 1: 从文件当前位置开启偏移
- 2: 从文件末尾开始偏移
- 移动的单位是字节(byte)一个字节不等于一个汉字
- 一个汉字由若干个字节构成,根据编码不同一个汉字的字节数也不同
- 返回文件只针对当前位置
with open(r'file01.txt', 'r' ) as f:
f.seek(6,0)
strChar = f.read()
print(strChar)
tell函数:用来显示文件读写指针的当前位置
with open(r'file01.txt', 'r' ) as f:
strChar = f.read(3)
pos = f.tell()
while strChar:
print(pos)
print(strChar)
strChar = f.read(3)
pos = f.tell()
文件的写操作-write
- write(str): 把字符串写入文件
- writelines(str): 把字符串按行写入文件,可以写入很多行,参数可以是list格式
- 区别:
- write 函数参数只能是字符串
- writelines 参数可以是字符串,也可以是字符序列
with open(r'file01.txt','a') as f:
f.write("李白-- \n 静夜思")
f.writelines("鹅 鹅 鹅")
l = ["I","love","dsjka"]
with open(r'file01.txt','w') as f:
f.writelines(l)
文件复制,删除,移动由os模块负责
持久化 - pickle
- 序列化 (持久化,落地):把程序运行中的信息保存在磁盘上
- 反序列化: 序列化的逆过程
- pickle : python提供的序列化模块
- pickle.dump: 序列化
- pickle.load: 反序列化
import pickle
age = 19
with open(r'file02.txt','wb') as f:
pickle.dump(age,f)
import pickle
with open(r'file02.txt','rb') as f:
age = pickle.load(f)
print(age)
19
people = [23,'whj','beijing',[175,120]]
with open(r'file02.txt', 'wb') as f:
pickle.dump(people,f)
with open(r'file02.txt', 'rb') as f:
people = pickle.load(f)
print(people)
[23, 'whj', 'beijing', [175, 120]]
持久化 - shelve
- 持久化工具
- 类似字典,用kv对保存数据,存储方式跟字典也类似
- open,close.有打开必须有关闭
import shelve
shv = shelve.open(r'shv.db')
shv['one'] = 1
shv['two'] = 2
shv['three'] = 3
shv.close()
import shelve
shv = shelve.open(r'shv.db')
try:
print(shv['one'])
print(shv['two'])
except Exception as e:
print("32")
finally:
shv.close()
1
2
shelve 特性
- 不支持多个应用并行写入
- 为了解决这个问题,open的时候可以使用flag=r
- 写回问题
- shelv 一般情况下不会等待持久化对象进行任何修改
- 解决方案:强制回写:writeback = true
import shelve
shv = shelve.open(r'shv.db',flag='r')
try:
k1 = shv['one']
print(k1)
finally:
shv.close()
{'eins': 1, 'zwei': 2, 'drei': 3}
import shelve
shv = shelve.open(r'shv.db')
try:
shv['one'] = {"eins":1,"zwei":2,"drei":3}
finally:
shv.close()
shv = shelve.open(r'shv.db')
try:
one = shv['one']
print(one)
finally:
shv.close()
{'eins': 1, 'zwei': 2, 'drei': 3}
import shelve
shv = shelve.open(r'shv.db',flag='r')
try:
k1 = shv['one']
print(k1)
k1["eins"] = 100
finally:
shv.close()
shv = shelve.open(r'shv.db')
try:
one = shv['one']
print(one)
finally:
shv.close()
{'eins': 1, 'zwei': 2, 'drei': 3}
{'eins': 1, 'zwei': 2, 'drei': 3}
import shelve
shv = shelve.open(r'shv.db',writeback=True)
try:
k1 = shv['one']
print(k1)
k1["eins"] = 100
finally:
shv.close()
shv = shelve.open(r'shv.db')
try:
one = shv['one']
print(one)
finally:
shv.close()
{'eins': 1, 'zwei': 2, 'drei': 3}
{'eins': 100, 'zwei': 2, 'drei': 3}
with shelve.open(r'shv.db',writeback=True) as shv:
k1 = shv['one']
print(k1)
k1["eins"] = 10000
with shelve.open(r'shv.db') as shv:
print(shv['one'])
{'eins': 1000, 'zwei': 2, 'drei': 3}
{'eins': 10000, 'zwei': 2, 'drei': 3}