look code
# Filename: classmates2.py import os,pickle class Friend: def __init__(self,name,age,phone): self.name=name self.age=age self.phone=phone def __str__(self): return '%s %d %s'%(self.name,self.age,self.phone) class Main: flist=[] # data list isChange=False # tip change operating isStart=True # tip start source='classmates' # source data name def __init__(self): hasData=os.path.isfile(Main.source+'.data') hasTxt=os.path.isfile(Main.source+'.txt') if hasData: Main.readFile(self,Main.source+'.data') elif hasTxt: Main.readTxtFile(self,Main.source+'.txt') Main.save(self) else: Main.writeFile(self,Main.source+'.data') Main.isStart=False # Start def start(self): Main.readme(self) while True: cmd=input('Command:') if cmd in ['exit','quit','q']: break; elif cmd in ['show','sh']: Main.show(self) elif cmd.startswith('find'): Main.show(self,Main.find(self,cmd.split(',')[1:])) elif cmd.startswith('add'): Main.add(self,cmd.split(',')[1:]) elif cmd.startswith('dele'): Main.delete(self,cmd.split(',')[1:]) elif cmd in['clear','clr','c']: Main.clear(self) elif cmd in ['save','sa']: Main.save(self) elif cmd.startswith('export'): Main.export(self) else: print('Please try again!') print('exit') # Show data def show(self,data=None): if data==None: data=Main.flist[:] if len(data)==0: print('no result') else: if len(data)==1: print(data[0]) else: print('list size %d:'%len(data)) for item in data: print(item) # Find data list by keys def find(self,keys): if len(keys)==0: return [] else: tmplist=[] for fri in Main.flist: if (fri.name in keys) or (str(fri.age) in keys) or (fri.phone in keys): tmplist.append(fri) return tmplist # Add data to flist def add(self,vals): if len(vals)==3: Main.flist.append(Friend(vals[0],int(vals[1]),vals[2])) Main.isChange=True if not Main.isStart: print('%s add success!'%vals[0]) else: print('the add command arguments is error, it must following 3 argument.') # Delete data by keys def delete(self,keys): tmplist=Main.find(self,keys) if len(tmplist)==0: print('no reslt') else: Main.flist=[i for i in Main.flist if not (i in tmplist)] Main.isChange=True print('has delete') Main.show(self,tmplist) def clear(self): if len(Main.flist)==0: print('no need') else: Main.flist.clear() Main.isChange=True print('clear success') # Save data to local def save(self): if Main.isChange: Main.isChange=False Main.writeFile(self,Main.source+'.data') if not Main.isStart: print('save success') else: print('No change') # Write informat to the path file def writeFile(self,path): file=open(path,'wb') pickle.dump(Main.flist,file) file.close() # Read the path file def readFile(self,path): file=open(path,'rb') Main.flist=pickle.load(file) file.close() # Read the path text file def readTxtFile(self,path): file=open(path,encoding='utf-8') line=file.readline() while True: line=file.readline() if len(line)==0: break Main.add(self,line.strip().split(' ')) # Export a text file def export(self,path=None): if not Main.isChange: print('No change') return if path==None: path=Main.source+'.txt' file=open(path,'w',encoding='utf-8') file.write(Main.parseStr(self)) file.close() os.system(path) print('exprot success') # Parse list to str def parseStr(self): rst='list:\n' for fri in Main.flist: rst+=str(fri)+'\n' return rst # User's manual def readme(self): print('You can input up command:') print(' exit/quit/q:exit the program') print(' show/sh:show the list') print(' find:find list by keys') print(' add:add new friend information') print(' dele:delete by keys') print(' clear/clr/c:clear the list') print(' save/sa:save the result') print(' export:export a text file and view the file') pass main=Main() main.start()