1、编写文件修改功能,调用函数时,传入三个参数(修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改
def file_change(src_file, src_info, dsc_info):
'''文件修改功能 参数:修改文件路径 要修改的内容 修改后的内容'''
with open(r'{}'.format(src_file), 'r', encoding='utf-8')as f:
res = f.read()
res.replace(f'{src_info}', f'{dsc_info}')
with open(r'{}'.format(src_file), 'w', encoding='utf-8')as f1:
f1.write(res)
return '修改成功'
2、编写tail工具
def tail_access(file_name):
''' 监控日志功能 '''
with open(f'{file_name}', 'rb')as f:
f.seek(0, 2)
while True:
res = f.readline()
if not res:
time.sleep(0.5)
else:
return res.decode('utf-8')
3、编写登录功能
def login():
while True:
user_name = input('请输入用户名:').strip()
user_pwd = input('请输入密码:').strip()
with open('user_info.txt', 'r', encoding='utf-8')as f:
for line in f:
username, password = line.strip().split(':')
if user_name == username and user_pwd == password:
return True, '登录成功'
else:
return False, '用户名或密码错误'
4、编写注册功能
def register():
user_name = input('请输入用户名:').strip()
user_pwd = input('请输入密码:').strip()
with open('user_info.txt', 'r', encoding='utf-8')as f:
res = f'{user_name}:{user_pwd}'
f.write(res)
return '注册成功!'
选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
4、查询余额功能:输入账号查询余额
选做题中的选做题:登录功能
用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作
import os
user_info = {
'username': None
}
def select(user_name):
'''用户登录校验功能'''
with open('user_info.txt', 'r', encoding='utf-8')as f:
for line in f:
username, password, balance = line.strip().split(':')
if user_name == username:
return username, password, balance
else:
return False
def receive_money(user_name, money):
'''用户接收转账功能'''
with open('user_info.txt', 'r', encoding='utf-8')as f1,
open('user_info1.txt', 'w', encoding='utf-8')as f2:
for line in f1:
username, password, balance = line.strip().split(':')
if user_name == username:
balance = int(balance)
balance += money
res = f'{username}:{password}:{balance}
'
f2.write(res)
os.remove('user_info.txt')
os.rename('user_info1.txt', 'user_info.txt')
def login():
'''登录功能'''
while True:
user_name = input('请输入用户名:').strip()
user_pwd = input('请输入密码:').strip()
flag = select(user_name)
if flag:
if user_pwd == flag[1]:
user_info['username'] = user_name
print('登录成功!')
break
else:
print('用户名或密码错误!')
else:
print('用户不存在!')
def recharge():
'''充值功能'''
if user_info['username']:
recharge_money = input('请输入充值金额:').strip()
recharge_money = int(recharge_money)
with open('user_info.txt', 'r', encoding='utf-8')as f1,
open('user_info1.txt', 'w', encoding='utf-8')as f2:
for line in f1:
username, password, balance = line.strip().split(':')
if user_info['username'] == username:
balance = int(balance)
balance += recharge_money
res = f'{username}:{password}:{balance}
'
f2.write(res)
os.remove('user_info.txt')
os.rename('user_info1.txt', 'user_info.txt')
else:
print('请先登录!')
login()
def withdraw():
'''提现功能'''
if user_info['username']:
with open('user_info.txt', 'r', encoding='utf-8')as f1,
open('user_info1.txt', 'w', encoding='utf-8')as f2:
for line in f1:
username, password, balance = line.strip().split(':')
if user_info['username'] == username:
while True:
money = input('请输入提现金额:').strip()
money = int(money)
balance = int(balance)
if balance >= money:
balance -= money
break
else:
print('用户余额不足!')
continue
res = f'{username}:{password}:{balance}
'
f2.write(res)
os.remove('user_info.txt')
os.rename('user_info1.txt', 'user_info.txt')
else:
print('请先登录!')
login()
def transfer():
'''转账功能'''
if user_info['username']:
while True:
to_name = input('请输入转账用户:').strip()
flag = select(to_name)
if flag:
with open('user_info.txt', 'r', encoding='utf-8')as f1,
open('user_info1.txt', 'w', encoding='utf-8')as f2:
for line in f1:
username, password, balance = line.strip().split(':')
if user_info['username'] == username:
while True:
to_money = input('请输入转账金额:').strip()
to_money = int(to_money)
balance = int(balance)
if balance >= to_money:
balance -= to_money
print('转账成功')
break
else:
print('用户余额不足!')
continue
res = f'{username}:{password}:{balance}
'
f2.write(res)
os.remove('user_info.txt')
os.rename('user_info1.txt', 'user_info.txt')
#调用转账用户接收功能
receive_money(to_name, to_money)
break
else:
print('转账用户不存在!')
else:
print('请先登录!')
login()
def check_balance():
'''查看余额功能'''
if user_info['username']:
with open('user_info.txt', 'r', encoding='utf-8')as f:
for line in f:
username,password , balance = line.strip().split(':')
if user_info['username'] == username:
print(f'用户{username}余额为:{balance}')
else:
print('请先登录!')
login()
def logout():
'''退出功能'''
if user_info['username']:
user_info['username'] = None
else:
print('用户没有登录!')
#功能函数
func_dic = {
'0': login,
'1': recharge,
'2': withdraw,
'3': transfer,
'4': check_balance,
'5': logout
}
def run():
while True:
print('''
'0',登录
'1',充值
'2',提现
'3',转账
'4',查询余额
'5',退出登录
''')
cmd = input('请输入功能编号:').strip()
if not cmd.isdigit():
print('请输入数字')
continue
elif cmd not in func_dic:
print('请输入正确的编号')
continue
else:
func_dic.get(cmd)()
if __name__ == '__main__':
run()