网络编程
位置:首页>> 网络编程>> Python编程>> 详解python持久化文件读写

详解python持久化文件读写

作者:Daecy  发布时间:2023-09-01 15:16:44 

标签:python,文件读写

持久化文件读写:


f=open('info.txt','a+')
f.seek(0)
str1=f.read()
if len(str1)==0:
 f1 = open('info.txt', 'w+')
 str1 = f.read()

# 如果数据没有就写入数据到文件


time_list = ["早上", "中午", "晚上"]
character_list = ["小赵","小钱", "小孙", "小李"]
place_list = ["在屋里", "在外面", "在学校", "在公司"]
event_list = ["吃饭", "遛狗", "看书", "工作"]
dic1={'time_list':time_list,'character_list':character_list,'place_list':place_list,'event_list':event_list}
str1=str(dic1)
f1.write(str1)
f1.close()
print("文件保存成功")

# 如果数据有,就字符串转换为字典


else:
 dic1=eval(str1)
 print(type(dic1))
 print(dic1)

f=open('info.txt','a+')
f.seek(0)
str1=f.read()
if len(str1)==0:
f1 = open('info.txt', 'w+')
str1 = f.read()

# 如果数据没有就写入数据到文件


time_list = ["早上", "中午", "晚上"]
character_list = ["小赵","小钱", "小孙", "小李"]
place_list = ["在屋里", "在外面", "在学校", "在公司"]
event_list = ["吃饭", "遛狗", "看书", "工作"]
dic1={'time_list':time_list,'character_list':character_list,'place_list':place_list,'event_list':event_list}
str1=str(dic1)
f1.write(str1)
f1.close()
print("文件保存成功")

# 如果数据有,就字符串转换为字典


else:
   dic1=eval(str1)
   print(type(dic1))
   print(dic1)

以上所述是小编给大家介绍的python持久化文件读写详解整合网站的支持!

来源:https://blog.csdn.net/weixin_44607176/article/details/88667106

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com