python高级语法 - 文件读写操作及其持久化

文件

  • 长久保存信息的一种数据信息集合(持久化)
  • 常用操作
    • 打开关闭(文件一旦打开,需要关闭操作)
    • 读写内容
    • 查找

open 函数

  • open函数负责打开文件,带有很多参数
    • 第一个参数:必须有,文件的路径和名称
    • mode:表明文件用什么方式打开(读,写)
      • r:以只读方式打开
      • w:写方式打开,会覆盖以前的内容
      • x:创建方式打开,如文件已经存储,报错
      • a:append方式,以追加的方式对文件内容进行写入
      • b:binary方式,二进制方式写入
      • t:文本方式打开
      • +:可读写
    • 注:若不指定文件路径,默认在本路径下创建文件
# 打开文件,用写的方式
# r表示后面字符串内容不需要转义
# f称之为文件句柄
f = open(r"test01.txt",'w')

#文件打开后必须关闭
f.close()
# 此案例说明,以写方式打开文件,默认是如果没有文件,则创建

with语句

  • with语句使用得技术是一种成为上下文管理协议得技术(ContextMangagementProtoal),由系统负责关闭文件。
  • 自动判断文件的作用域,自动关闭不在使用的打开的文件句柄
  • 对文件的使用一般要求使用with打开文件
with open(r"test01.txt",'r') as f:
    pass
    # 下面语句块开始对文件f进行操作
    # 在本模块中不需要在使用close关闭文件f

readline

  • 一行一行读取文件
# with 案例
with open(r"file01.txt",'r') as f:
    # 按行读取内容
    strline = f.readline()
    # 此结构保证能够完整读取文件直到结束
    while strline:
        print(strline)
        strline = f.readline()

list

  • 整个文件读取,把文件内每一行作为一个元素,进行遍历
# 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)一个字节不等于一个汉字
  • 一个汉字由若干个字节构成,根据编码不同一个汉字的字节数也不同
  • 返回文件只针对当前位置
# seek案例
# 打开文件后,从第6个字节开始读取

# 打开后默认文件指针字0处,即文件的开头
with open(r'file01.txt', 'r' ) as f:
    # seek移动单位是字节
    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 参数可以是字符串,也可以是字符序列
# write 案例
# 向文件追加诗的名称

# a 代表追加方式打开

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:
    # 将 age 以二进制方式序列化到file02.txt中
    pickle.dump(age,f)
# 反序列化案例

import pickle

with open(r'file02.txt','rb') as f:
    age = pickle.load(f)
    print(age)
19
# 序列化案例2

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.有打开必须有关闭
# 使用shelve创建文件并使用
import shelve

# 打开文件
# shv相当于一个字典
shv = shelve.open(r'shv.db')
shv['one'] = 1
shv['two'] = 2
shv['three'] = 3

shv.close()

# 通过以上案例发现,shelve自动创建的不仅仅是一个shv.db文件,还包括其他的文件
# shelve 读取案例
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
# shelve 以只读打开
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}
# shelve 使用强制写回

import shelve

shv = shelve.open(r'shv.db',flag='r')

try:
    k1 = shv['one']
    print(k1)
    # 此时,一旦shelve关闭,则内容还在内存中,没有回写数据库
    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}
# shelve 使用强制写回

import shelve

shv = shelve.open(r'shv.db',writeback=True)
try:
    k1 = shv['one']
    print(k1)
    # 通过writeback 强制回写
    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}
# shelve 使用with管理上下文环境

with shelve.open(r'shv.db',writeback=True) as shv:
    k1 = shv['one']
    print(k1)
    # 通过writeback 强制回写
    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}

猜你喜欢

转载自blog.csdn.net/july_whj/article/details/80863005