022测试对文档进行读取与写入

import time
LoginTime = time.asctime( time.localtime(time.time()) )
print ("time %s" % LoginTime)
count = 0
data = ("1注册新会员","2.会员登录")
print (data[0],data[1])
choice = input("请选择1或2(按其它键将退出):")
#if choice == "q":
# print("再见!")
if choice.isdigit():
choice = int(choice)
with open("yesterday01","a+" ,encoding="utf-8") as f,open("yesterday01bak","a+" ,encoding="utf-8") as f_bak:
if choice == 1:
username =input("请输入用户名:")
pwd = input("请输入密码:")
print ("注册成功!")
f.write("%s%s "% (username,pwd))
exit()
elif choice ==2:
for count in range(1,4):
user_name = input("请输入用户名:")
user_passwd = input("请输入密码:")
with open("yesterday01","r+" ) as f,open("yesterday01bak","a+" ,encoding="utf-8") as f_bak:
for line in f:
count2 = 3-count
if user_name and user_passwd in line.strip():
f_bak.write("%s,%s登录成功! "%(LoginTime,user_name))
print("登录成功!")
exit()
elif user_name in line.strip() and user_passwd not in line.strip():
f_bak.write("%s,%s密码不对,第%s次登录失败! "%(LoginTime,user_name,count))
if count2 > 0:
print("用户名或密码不对,你还有%s次登录机会!"% count2)
elif user_name not in line:
f_bak.write("%s,%s用户不存在,第%s次登录失败! "%(LoginTime,user_name,count))
if count2 > 0:
print("%s用户不存在,你还有%s次登录机会!"% (user_name,count2))
# else:
# f_bak.write("%s,%s第%s次登录失败! "%(LoginTime,user_name,count))
# print("用户名或密码不对,你还有%s次登录机会!"% count2)
else:
print("你已超过了登录次数!byebye!")

else:
print("再见!")
原文地址:https://www.cnblogs.com/liulvzhong/p/6428841.html