day13_作业



# 1、编写文件修改功能,调用函数时,传入三个参数、
# (修改的文件路径,要修改的内容,修改后的内容)既可完成文件的修改

# def modify_file(res_file,old_file,new_file):
# with open(res_file,mode='rt',encoding='utf-8') as f1:
# # res=f1.readline()
# res=f1.read()
#
# with open(res_file,mode='wt',encoding='utf-8') as f2:
# f2.write(res.replace(old_file,new_file))
# return print('修改成功')
# modify_file('a.txt','bbb','aaa')




import os
#
# #函数定义
def change_file(file_path,old,new):
'''
:param file_path: 修改文件的路径
:param old: 要修改的内容
:param new: 修改后的内容
:return:
'''
#1)先将文件中的数据读取出来
#file_path: E:\Python\s14\day13\test.txt
#方式一:操作系统同时打开了两个文件,同时占用系统两个资源
# with open(file_path,mode='rt',encoding='utf-8') as f,\
# open('copy.txt',mode='wt',encoding='utf-8')as w:
# for line in f:
# line=line.replace(old,new) # old:tank3, new:egon
# w.write(line)

# 将修改后的copy.txt文件,改名为file_path
# os.rename('copy.txt',file_path)


#方式二:只打开一个文件
list1=[]
with open(file_path,mode='rt',encoding='utf-8') as f:
for line in f:
line=line.replace(old,new) # old:tank3, new:egon
list1.append(line)

#执行到此处,操作系统已经回收文件资源
with open(file_path, mode='wt', encoding='utf-8') as f:
for line in list1:
f.write(line)


# 函数调用:
change_file(
'E:/Python/s14/day13/test.txt', #注意windows系统下,拷贝文件的路径分隔符是"\",
'tank3', #在这里会报错,提示访问不了这个文件,改成"/"即可
'egon'
)


# import os
# res_file=input('请输入要要修改文件的绝对路径:')
# with open(r'{}'.format(res_file),mode='rt',encoding='utf-8') as f,\
# open(r'.c.txt.swap',mode='wt',encoding='utf-8') as f1:
#
# # res=f.readline()
# # print('修改前的内容:', res)
# # while True: #一次读一行
# # line=f.readline()
# # if len(line) == 0:
# # break
# # print('修改前文件内容:',line,end='')
#
# for line in f:
# f1.write(line.replace('alex','dsb'))
#
# os.remove('{}'.format(res_file))
# os.rename('.c.txt.swap','c.txt')
#
# # with open(r'{}'.format(res_file),mode='rt',encoding='utf-8') as f2:
# # res1=f2.readline()
# # print('修改后的内容:',res1)

## while True: #一次读一行
## line=f2.readline()
## if len(line) == 0:
## break
## print('修改后文件内容:',line,end='')


# 2、编写tail工具
# import time
# with open('access.txt',mode='rt',encoding='utf-8') as f:
# f.seek(0,2)
# while True:
# res=f.readline()
# if len(res) == 0:
# time.sleep(0.3)
# else:
# print(res)




#3 登录功能:
# def login():
# uesrname=input('用户名:').strip()
# userpwd=input('密码:').strip()
# if uesrname == '123' and userpwd == '123':
# print('登录成功!')
# else:
# print('登录失败!')
#
# #函数调用:
# login()


#注册功能:
# def register():
# uesrname=input('用户名:').strip()
# userpwd=input('密码:').strip()
# reuserpwd=input('确认密码:').strip()
# if userpwd ==reuserpwd:
# # print(f'{uesrname}注册成功')
# print('{}注册成功!'.format(uesrname))
#
# #函数调用:
# register()


# 选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改

def pay_money(username):
#1)将db.txt文件中所有的用户数据读取出来存放在字典中
dic={} #用于存放用户名和金额
with open('db.txt',mode='rt',encoding='utf-8') as f:
for line in f:
user,money=line.strip().split(':')
dic[user]=int(money)

#2)校验当前传入的username是否存在,若不存在则结束程序
if username not in dic:
print('用户不存在,结束程序!')
return

#3)循环让用户输入充值的金额
while True:
money=input('请输入充值金额:').strip()
if not money.isdigit():
print('请输入数字!')
continue
money=int(money)

#4)给当前用户金额加钱
dic[username] += money #通过字典的key拿到value,即金额,然后再加上充值的金额


#5)将修改后的用户数据写入文件中

with open('db.txt',mode='wt',encoding='utf-8') as f:
for user,money in dic.items():
f.write(f'{user}:{money}\n')

break

pay_money('tank')


# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱

# def transfer(A_user,B_user,transfer_money):
# # if not user_info.get('user'):
# # login()
#
# dic={}
# with open('db2.txt',mode='rt',encoding='utf-8') as f:
# for line in f:
# user,money=line.strip().split(':')
# dic[user]=int(money)
#
# #判断转账用户是否存在
# if A_user not in dic :
# print('转账用户不存在')
# return
#
# if B_user not in dic:
# print('收款用户不存在')
# return
#
# print('转账前:',dic )
# #1)判断转账用户金额是否大于等于转账金额
# if dic.get(A_user) >= transfer_money:
# #转账用户扣钱
# dic[A_user] -= transfer_money
#
# #收款用户加钱
# dic[B_user] += transfer_money
# print('转账后:',dic)
#
# #2)此时转账与收款用户的数据都已经修改过了,重新写入文件中
# with open('db2.txt',mode='wt',encoding='utf-8') as f:
# for user,money in dic.items():
# f.write(f'{user}:{money}\n')

#函数调用
# transfer('egon','tank',1000)



# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# 4、查询余额功能:输入账号查询余额



posted @ 2020-12-30 16:22  欢乐二次方  阅读(118)  评论(0)    收藏  举报