#1、通用文件copy工具实现
file=input('原文件路径:')
c_file=input('目标文件文件路径:')
with open(r'{}'.format(file),mode='rb')as x,\
open(r'{}'.format(c_file),mode='wb')as y:
for z in x:
print(z)
y.write(z)
#2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容
with open(r'D:\cool\user2.txt',mode='r+',encoding='utf-8') as f:
f.seek(5,0)
print (f.read())
with open(r'D:\cool\user1.txt', mode='w+', encoding='utf-8') as f:
f.write('321')
f.seek(2,0)
print(f.tell())
f.write('啦啦啦')
with open(r'D:\cool\user2.txt',mode='a+',encoding='utf-8') as f:
f.seek(4,0)
f.write('哈哈')
#3、tail -f access.log程序实现
import time
with open('access.log',mode='rb') as f:
f.seek(0,2)
while 1:
line=f.readline()
if len(line) == 0:
time.sleep(0.5)
else:
print(line.decode('utf-8'),end='')
#4、周末作业参考在老师讲解完毕后(下午17:30开始讲解),练习熟练,明早日考就靠他俩
# 4.1:编写用户登录接口
# 4.2:编写程序实现用户注册后(注册到文件中),可以登录(登录信息来自于文件)
tag = True
while tag :
dic = {}
with open(r'D:\cool\user', mode='rt', encoding='utf-8') as x:
# print(x.read())
for y in x:
name, password, count = y.strip().split(':')
dic[name] = [password, count]
dic.get(name)[1] = int(dic.get(name)[1])
cmd = input('--0 退出--1 登录--2 注册--\n请输入命令编号>>:')
if cmd == '0':
print('退出')
break
elif cmd == '1':
while tag:
inp_name = input('请输入您的用户名:')
inp_pwd = input('请输入您的密码:')
if inp_name in dic:
if dic.get(inp_name)[1] >= 3:
print('该账户已锁定')
tag = False
elif inp_pwd == dic.get(inp_name)[0]:
print('登录成功')
tag = False
elif inp_pwd != dic.get(inp_name)[0]:
print('账号或密码 错误')
dic.get(inp_name)[1] += 1
print(f'用户名{inp_name} 已错误{dic.get(inp_name)[1]}次')
dic1 = str(dic)
z = dic1.strip("{}'[]").replace("': ['", ":").replace("', ", ":").replace("], '", "\n")
with open(r'D:\cool\user', mode='wt', encoding='utf-8') as a:
a.write(z)
if dic.get(inp_name)[1] == 3:
print('锁定')
tag = False
else:
print('用户名不存在\n返回 请按[2]\n退出 请按[0]')
cmd2 = input('请输入命令编号>>:')
if cmd2 == '2':
break
elif cmd2 == '0':
print('退出')
tag = False
elif cmd == '2':
while tag:
dic2 = {}
with open(r'D:\cool\user',mode='rt',encoding='utf-8') as i:
# print(x.read())
for o in i:
name,password,count=o.strip().split(':')
dic2[name]=[password,count]
dic2.get(name)[1] = int(dic2.get(name)[1])
inp_user = input('请输入您想注册的用户名:')
inp_password = input('请输入你的注册密码:')
inp_password2 = input('请再次输入你的注册密码:')
# print(dic2)
if inp_user in name:
print('该用户名已被注册')
break
else:
if inp_password == inp_password2:
with open(r'D:\cool\user', mode='at', encoding='utf-8') as z:
z.write(f'\n{inp_user}:{inp_password}:0')
print('注册成功')
break
else:
print('注册密码有误\n请重新输入')
else:
print('命令编号有误\n请输入正确编号')