2020-06-15
# 1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
def modify(file_path, old_contents, new_contents): with open(r'%s' % file_path, mode='r', encoding='utf-8') as f1, open(r'new_file.txt', mode='w', encoding='utf-8') as f2: data = f1.read() res = data.replace(old_contents, new_contents) f2.write(res) import os os.remove(file_path) os.rename('new_file.txt', file_path) print("修改完成")
# 2、编写tail工具
def tail(): import time with open("a.txt", mode='rb')as f: f.seek(0, 2) while True: line = f.readline() if len(line) == 0: time.sleep(0.1) else: print(line.decode('utf-8'), end="")
# 3、编写登录功能
def login(): i = 0 while i < 3: inp_name = input("请输入您的用户名:") inp_psw = input("请输入您的密码:") with open(r'registration.txt', mode='rt', encoding='utf-8') as f3: for line in f3: name, psw = line.strip(' ').split(':') if inp_name == name and inp_psw == psw: print("登录成功!") i = 3 break else: print("登录失败,请重新登录!") i += 1
# 4、编写注册功能
def register(): tag = True while tag: l = [] inp_name = input("请注册您的用户名:").strip() with open('registration.txt', mode='r', encoding='utf-8') as f1: for line in f1: info_name, *_ = line.strip(' ').split(':') l.append(info_name) if inp_name not in l: while tag: inp_psw = input("请注册您的密码(仅由数字组成):").strip() if inp_psw.isdigit(): with open('registration.txt', mode='a', encoding='utf-8') as f2: f2.write('%s:%s' % (inp_name, inp_psw)) print("注册成功!") tag = False else: print("密码格式错误") else: print("用户名已存在,请重新输入")
# 5、编写用户认证功能
def check(): tag = True while tag: inp_name = input("请输入您的用户名:") inp_psw = input("请输入您的密码:") with open(r'registration.txt', mode='rt', encoding='utf-8') as f3: for line in f3: name, psw = line.strip(' ').split(':') if inp_name == name and inp_psw == psw: print("认证成功!") tag = False break else: print("认证失败")
# 选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# 4、查询余额功能:输入账号查询余额
# 选做题中的选做题:登录功能
# 用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作