python 自学day2 对文件的增删查

代码

import json
def search(file,str1,str2=''):
count = 0
k = 0
with open(file,'r+',encoding='utf-8') as f1:
for i in f1:
count +=1
if str1 in i:
k = count+1
print(i)
if count == k:
k += 1
if str2 not in i :
print(i)
else:
k -= 1
def add(file,**arg):
p = json.dumps(arg)
with open(file,'a',encoding='utf-8') as f1:
f1.write(p)
def delete(file,file1,str1):
with open(file,'r+',encoding='utf-8') as f1,\
open(file1, 'r+',encoding='utf-8') as f2:
for i in f1:
if str1 in i:
continue
f2.write(i)

猜你喜欢

转载自www.cnblogs.com/czh96/p/11252136.html