文件
- 长久保存信息的一种数据信息集合
- 常用操作
- 打开关闭(文件一旦打开,需要关闭操作)
- 读写内容
- 查找
open函数
- open函数负责打开文件,带有很多参数
- 第一个参数: 必须有,文件的路径和名称
- mode:表明文件用什么方式打开
- r:以只读方式打开
- w:写方式打开,会覆盖以前的内容
- x:创建方式打开,如文件已经存在,报错
- a:append方式,以追加的方式对文件内容进行写入
- b: binary方式,二进制方式写入
- t: 文本方式打开
- +: 可读写
f = open(r"test01.txt", 'w')
f.close()
with语句
- with语句使用的技术是一种成为上下文管理协议的技术(ContextManagementProtocal)
- 自动判断文件的 作用域, 自动关闭不在使用的打开的文件句柄
with open(r"test01.txt", 'r') as f:
pass
with open(r'test01.txt', 'r') as f:
strline = f.readline()
while strline:
print(strline)
strline = f.readline()
假若他日相逢
我将何以贺你
以沉默
以眼泪
with open(r'test01.txt', 'r') as f:
l = list(f)
for line in l:
print(line)
假若他日相逢
我将何以贺你
以沉默
以眼泪
with open(r'test01.txt', 'r') as f:
strChar = f.read(1)
print(len(strChar))
print(strChar)
1
假
seek(offset, from)
- 移动文件的读取位置,也叫读取指针
- from的取值范围:
- 0: 从文件头开始偏移
- 1:从文件当前位置开始偏移
- 2: 从文件末尾开始偏移
- 移动的单位是字节(byte)
- 一个汉子由若干个字节构成
- 返回文件只针对 当前位置
with open(r'test01.txt', 'r') as f:
f.seek(6, 0)
strChar = f.read()
print(strChar)
他日相逢
我将何以贺你
以沉默
以眼泪
import time
with open(r'test01.txt', 'r') as f:
strChar = f.read(3)
while strChar:
print(strChar)
time.sleep(1)
strChar = f.read(3)
假若他
日相逢
我将
何以贺
你
以
沉默
以眼泪
with open(r'test01.txt', 'r') as f:
strChar = f.read(3)
pos = f.tell()
while strChar:
print(pos)
print(strChar)
strChar = f.read(3)
pos = f.tell()
9
假若他
18
日相逢
25
我将
34
何以贺
41
你
以
48
沉默
57
以眼泪
文件的写操作-write
- write(str): 把字符串写入文件
- writeline(str): 把字符串按行写入文件
- 区别:
- write函数参数只能是字符串
- writerline参数可以是字符串,也可以是字符序列
with open(r'test01.txt', 'a') as f:
f.write("生活不仅有眼前的苟且, \n 还有远方的苟且")
with open(r'test01.txt', 'a') as f:
f.writelines("生活不仅有眼前的苟且")
f.writelines("还有远方的枸杞")
l = ["I", "love", "wangxiaojing"]
with open(r'test01.txt', 'w') as f:
f.writelines(l)
持久化 - pickle
- 序列化(持久化,落地):把程序运行中的信息保存在磁盘上
- 反序列化: 序列号的逆过程
- pickle: python提供的序列化模块
- pickle.dump:序列化
- pickle.load:反序列化
import pickle
age = 19
with open(r'test01.txt', 'wb') as f:
pickle.dump(age, f)
import pickle
with open(r'test01.txt', 'rb') as f:
age = pickle.load(f)
print(age)
19
import pickle
a = [19, 'liudana', "i love wangxiaojing", [185, 76]]
with open(r'test01.txt', 'wb') as f:
pickle.dump(a, f)
with open(r'test01.txt', 'rb') as f:
a = pickle.load(f)
print(a)
[19, 'liudana', 'i love wangxiaojing', [185, 76]]
持久化-shelve
- 持久化工具
- 类似字典,用kv对保存数据,存取方式跟字典也类似
- open, close
import shelve
shv = shelve.open(r'shv.db')
shv['one'] = 1
shv['two'] = 2
shv['three'] = 3
shv.close()
shv = shelve.open(r'shv.db')
try:
print(shv['one'])
print(shv['threee'])
except Exception as e:
print("烦死了")
finally:
shv.close()
1
烦死了
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()
1
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}
shv = shelve.open(r'shv.db')
try:
k1 = shv['one']
print(k1)
k1["eins"] =100
finally:
shv.close()
shv = shelve.open(r'shv.db')
try:
k1 = shv['one']
print(k1)
finally:
shv.close()
{'eins': 1, 'zwei': 2, 'drei': 3}
{'eins': 1, 'zwei': 2, 'drei': 3}
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:
k1 = shv['one']
print(k1)
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"] =1000
with shelve.open(r'shv.db') as shv:
print(shv['one'])
{'eins': 100, 'zwei': 2, 'drei': 3}
{'eins': 1000, 'zwei': 2, 'drei': 3}