Commit 24cde240 by BellCodeEditor

auto save

parent b72ce93c
Showing with 26 additions and 11 deletions
files=open('c:\Users\Admin\Desktop\test.txt','w',encoding="utf-8") files=open(r'c:\Users\Admin\Desktop\test.txt','w',encoding="utf-8")# w 创建文件
files.write("小兰:12本") files.write("小兰:12本\n")#写入数据
files.write("小丽:11本") files.write("小丽:11本\n")
files.write("李文:9本") files.write("李文:9本\n")
files.write("张伟:16本") files.write("张伟:16本\n")
files.close() files.close()#关闭文件
files=open('c:\Users\Admin\Desktop\test.txt','a',encoding="utf-8") with open(r'c:\Users\Admin\Desktop\test.txt','a',encoding="utf-8") as files:#a 追加
files.write("李明:11本") files.write("李明:11本\n")
files.write("小强:15本") files.write("小强:15本\n")
files.close()
# with open (r'c:\Users\Admin\Desktop\test.txt','r',encoding="utf-8") as files: #只读
# a=files.read()
# print(a)
new_files=''
with open (r'c:\Users\Admin\Desktop\test.txt','r',encoding="utf-8") as files:
for i in files:#for 循环遍历
if "小强:15本" in i:#判断数据是否在文本中
i = i.replace("小强:15本","小强:11本")#replace 修改数据
new_files+=i
print(new_files)
with open (r'c:\Users\Admin\Desktop\test.txt','w',encoding="utf-8") as files:
files.write(new_files)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment