'''
#1、通用文件copy工具实现
old_file = input('请输入原文件地址:').strip()
new_file = input('请输入目标地址:').strip()
with open(r'%s' %old_file,'rb') as rf, open(r'%s' %new_file, 'wb') as wf:
for line in rf:
wf.write(line)
#2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容
with open('a.txt', 'r+', encoding='utf-8') as rf:
rf.seek(5,0)
print(rf.read())
rf.write('ko')
with open('a.txt', 'w+', encoding='utf-8') as wf:
wf.seek(0,1)
wf.write('hhhoo你好')
print(wf.read())
with open('a.txt', 'a+', encoding='utf-8') as wf:
wf.seek(-6,2)
wf.write('hhhoo你好')
print(wf.read())
#3、tail -f access.log程序实现
import time
for i in range(1000000):
with open('access.log', 'at', encoding='utf-8') as f:
t = time.strftime('%Y-%m-%d %H:%M:%S')
msg = '我刚刚赚了%s个亿' % i
n =f'{t} {msg}\n'
f.write(n)
time.sleep(3)
import time
with open('access.log', 'rb') as f:
f.seek(0,2)
while True:
line = f.readline()
if len(line) == 0:
time.sleep(1)
else:
print(line.decode('utf-8'))
#4、
# 4.1:编写用户登录接口
inp_name = input('your name:').strip()
inp_pwd = input('your password:').strip()
with open('db.txt','r',encoding='utf-8') as f:
for i in f:
name,pwd = i.strip('\n').split(':')
if name == inp_name and pwd == inp_pwd:
print('login successful!')
break
else:
print('name or password error!')
# 4.2:编写程序实现用户注册后(注册到文件中),可以登录(登录信息来自于文件)
print('注册'.center(50,'='))
with open('db.txt', 'a', encoding='utf-8') as af:
inp_name = input('your name:').strip()
inp_pwd = input('your password:').strip()
msg = '%s:%s\n'%(inp_name,inp_pwd)
af.write(msg)
print('注册成功!')
print('登录'.center(50,'='))
with open('db.txt','r',encoding='utf-8') as rf:
user_name = input('your name:').strip()
user_pwd = input('your password:').strip()
for line in rf:
name,pwd = line.strip('\n').split(':')
if name == user_name and pwd == user_pwd:
print('login successful!')
break
else:
print('name or password error!')
#5、下属两个案例的升级需求完成
# 示范1:注册功能
# name = input("your name: ").strip()
# 做合法性校验:
# 1、如果输入的用户名包含特殊字符^$&...让用户重新输入
# 2、如果输入的用户名已经存在也重新输入
# pwd = input("your password: ").strip()
# 做合法性校验:
# 1、密码长度
# 2、如果密码包含特殊字符则重新输入
# f.txt = open('user.txt',mode='at',encoding='utf-8')
# f.txt.write('%s:%s\n' %(name,pwd))
# f.txt.close()
print('注册'.center(50,'='))
tag = True
while tag:
inp_name = input('your name:').strip()
if inp_name.isalnum():
with open('db.txt', 'r', encoding='utf-8') as rf:
for line in rf:
name, pwd = line.strip('\n').split(':')
if inp_name in name:
print('用户名重复,请重新输入')
break
else:
while True:
with open('db.txt', 'a', encoding='utf-8') as af:
inp_pwd = input('your password:').strip()
if inp_pwd.isalnum():
if len(inp_pwd) < 6:
print('密码太短,请重新输入')
else:
msg = '%s:%s\n' % (inp_name, inp_pwd)
af.write(msg)
print('注册成功!')
tag = False
break
else:
print('包含非法字符,请重新输入')
else:
print('包含非法字符,请重新输入')
'''
# 示范2:登录功能
# 升级需求1:同一个账号输错三次则退出
# 升级需求2:同一个账号输错三次则,该账号则锁定10秒,即便程序被终止,仍然计时
# import time
# t = time.strftime('%S')
# print(t)
#
d = {}
tag = True
while tag:
inp_name = input("your name: ").strip()
inp_pwd = input('your password:').strip()
d[inp_name] = {'error': 0}
with open('db.txt', 'r') as f:
for line in f:
user, pwd = line.strip('\n').split(':')
if inp_name == user:
if inp_pwd == pwd:
print('login successful')
tag = False
break
else:
d[inp_name]['error'] += 1
print('user or password error')
print(d)
break