import time
def show_info():
print("输入提示数字,执行相应操作:0.退出 1.查看登录日志")
# 记录日志
def write_logininfo(username):
with open("log.txt", "a", encoding='UTF-8') as file:
s = f"用户名:{username},登录时间:{time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())}"
file.write(s)
file.write('\n')
# 读取日志
def read_logininfo():
with open("log.txt", "r", encoding='UTF-8') as file:
while 1:
line = file.readline()
if line == "":
break
else:
print(line, end='')
if __name__ == '__main__':
username = input("请输入用户名:")
pwd = input("请输入密码:")
if username == "admin" and pwd == "12345":
print("登录成功!")
write_logininfo(username)
show_info()
num = int(input("请输入操作数字:"))
while 1:
if num == 0:
print("退出成功")
break
elif num == 1:
print("查看登录日志")
read_logininfo()
break
else:
print("您输入的数字有误!请重新输入!")
show_info()
num = int(input("输入操作数字:"))
else:
print("对不起,用户名或密码不正确!")
Python 记录用户登录日志
猜你喜欢
转载自blog.csdn.net/qq_46620129/article/details/113012846
今日推荐
周排行