制作一个"密码薄",其可以存储一个网址,和一个密码(如 123456),请编写程序完成这个“密码薄”的增删改查功能,并且实现文件存储功能
D:\pytest_day\mimab\wenjian.py
class WenJian(): def __init__(self,lujing,xieru): self.lujing=lujing self.xieru=xieru #读文件 def read_file(self): #打开文件 with open(self.lujing,'r') as f: content = [] # 读取内容 for i in f: i=i.split('\n') content.append(i[0]) return content #写文件 def write_file(self): # f = open(self.lujing, 'w') with open(self.lujing, 'w') as f: for i in self.xieru: f.writelines(i+'\n') #关闭文件 def close_file(self): open(self.lujing).close()
D:\pytest_day\mimab\base.py
from mimab.wenjian import WenJian class Base_Wenjian(WenJian): def __init__(self,lujing,xieru): WenJian.__init__(self,lujing,xieru) #添加 def add(self): self.xieru = WenJian.read_file(self) url=input("请输入网址:") if url not in self.xieru: pwd=input("请输入密码:") self.xieru.append(url+' '+pwd) WenJian.write_file(self) WenJian.close_file(self) else: print("您输入的网址已经存在") #删除 def dell(self): self.xieru = WenJian.read_file(self) url = input("请输入要删除网址:") for i in self.xieru: a=i.split() if url == a[0]: self.xieru.remove(i) WenJian.write_file(self) WenJian.close_file(self) print("删除成功!") break else: print("您输入的网址不存在") #编辑 def edit(self): self.xieru = WenJian.read_file(self) url = input("请输入要编辑网址:") for j in range(len(self.xieru)): a=self.xieru[j].split() if url == a[0]: newurl = input("请输修改后的网址:") newpwd = input("请输修改后的密码:") self.xieru[j]=newurl+' '+newpwd WenJian.write_file(self) WenJian.close_file(self) print("修改成功!") break else: print("您输入的网址不存在") #查找 def find(self): self.xieru = WenJian.read_file(self) url = input("请输入要查找的网址:") for j in range(len(self.xieru)): a=self.xieru[j].split() if url == a[0]: print(self.xieru[j]) WenJian.write_file(self) WenJian.close_file(self) break else: print("输入的网址不存在")
D:\pytest_day\mimab\zsgc.py
from mimab.base import Base_Wenjian class Wenjian_Caozuo(Base_Wenjian): def __init__(self,lujing,xieru,index): Base_Wenjian.__init__(self,lujing,xieru) self.index=index def zsgc(self): if self.index == '1': #添加 Base_Wenjian.add(self) elif self.index == '2': #删除 Base_Wenjian.dell(self) elif self.index == '3': #修改 Base_Wenjian.edit(self) elif self.index == '4': #查找 Base_Wenjian.find(self) elif self.index == '5': print("欢迎下次使用!!!") exit() else: print("输入操作编号格式不对")