演示
代码
print('记录用户登录日志----------------------------')
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(time.time()))}'
file.write(s+'\n')
def read_logininfo(username):
with open('log.txt','r',encoding='utf-8')as file:
while True:
line=file.readline()
if line=='':
break
else:
print(line,end='')
if __name__ == '__main__':
username=input('请输入用户名:')
pwd=input('请输入密码:')
if username=='admin' and pwd=='admin':
print('登录成功!!!!')
write_logininfo(username)
show_info()
while True:
num=input('输入操作代码0/1:')
if num=='0':
print('退出成功')
break
elif num=='1':
print('查看登录信息')
read_logininfo(username)
continue
else:
print('您输入的数据有误!!')
show_info()
continue
else:
print('用户名或密码不正确')
'''print(time.time())#-----------获得秒
print(time.localtime(time.time()))#-----转换成tm_year=2021, tm_mon=11, tm_mday=23, tm_hour=13, tm_min=43, tm_sec=51, tm_wday=1, tm_yday=327, tm_isdst=0
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())))#-----转换成2021-11-23 13:49:18格式'''