1. 编写文件copy工具
ori_file=input('原文件路径:').strip() cop_file=input('新文件路径:').strip() with open(r'{}'.format(ori_file), mode='rt', encoding='utf-8') as f1,\ open(r'{}'.format(cop_file),mode='wt',encoding='utf-8') as f2: res = f1.read() f2.write(res)
2. 编写登录程序,账号密码来自于文件
print('欢迎来到登录界面'.center(50,'*')) acc = input('请输入账号:').strip() pwd = input('请输入密码:').strip() with open(r'account.txt',mode='rt',encoding='utf-8') as f1: for line in f1: account,password=line.strip().split(':') if acc in account and pwd in password: print('登录成功') break else: print('登陆失败') print('欢迎下次光临'.center(50,'*'))
3. 编写注册程序,账号密码来存入文件
print('欢迎来到注册界面'.center(50, '*')) acc = input('请输入注册账号:').strip() pwd = input('请输入注册密码:').strip() with open(r'account.txt', mode='at', encoding='utf-8') as f1: f1.write('{}:{}\n'.format(acc, pwd))
4. 编写用户登录接口
'''1.输入账号密码完成验证,验证通过后输出"登录成功" 2.可以登录不同的用户 3.同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定) 4.可以选择退出、注册、登录''' print('操作界面'.center(50, '='), end=' ') info = ''' 请选择以下操作: 1. 注册 2. 登录 0. 退出 ''' print(info) done = True while done: # 判断用户即将想要进行的操作 operate = input('输入序号选择您需要的操作:') operate.strip() if operate == '1': # 注册 register = True while register: # while register: account_register = input('请输入您注册的账号') account_register.strip() password_register = input('请输入您注册的密码') password_register.strip() with open(r'account.txt', mode='rt', encoding='utf-8') as f1: for line in f1: account, password = line.strip().split(':') if account_register == account: print('账户已存在,请重新输入') break else: with open(r'account.txt', mode='at', encoding='utf-8') as f2: f2.write('\n{}:{}'.format(account_register, password_register)) print('注册成功') register = False elif operate == '2': # 登录 n = 3 account_login = input('账号:') account_login.strip() while n > 0: # 可输入三次密码 password_login = input('密码:') password_login.strip() with open(r'close.txt', mode='rt', encoding='utf-8') as f5: #判断账号是否在黑名单中 for line in f5: account_close = line.strip() if account_login == account_close: print('该账号已锁定,请重新输入') continue with open(r'account.txt', mode='rt', encoding='utf-8') as f3: # 判断账号是否正确 n -= 1 for line in f3: account, password = line.strip().split(':') if account == account_login and password == password_login: print('登录成功') print('给老子跳段舞吧') # pass exit() elif n == 0: # 账号错误三次被锁定 print('该账号已连续输入错误三次,已被锁定') with open(r'close.txt',mode='at',encoding='utf-8') as f4: f4.write('{}\n'.format(account_login)) break else: print('账号密码错误,请重新输入') else: # 选择其他符号,直接退出程序 print('程序即将退出') print('拜拜,小*'.center(50, '=')) exit()
5. 通用文件copy工具实现
with open(r'a.txt', mode='rb') as f1,\ open(r'b.txt', mode='wb') as f2: res = f1.read() f2.write(res)
6. 基于seek控制指针移动,测试r+、w+、a+模式下的读写内容
with open(r'a.txt',mode='r+t',encoding='utf-8') as f1: print(f1.read()) f1.seek(5,0) print(f1.tell()) f1.write('asd') f1.seek(0,0) print(f1.read())
with open(r'b.txt',mode='w+t',encoding='utf-8') as f2: print(f2.read()) f2.seek(10,0) print(f2.tell()) f2.write('asd') f2.seek(0,0) print(f2.read())
with open(r'c.txt',mode='a+t',encoding='utf-8') as f3: print(f3.read()) f3.seek(5,0) print(f3.tell()) f3.write('asd') f3.seek(0,0) print(f3.read())
7. tail -f access.log程序实现
tag = True while tag: print('输入任意文字可添加至日志,输入readlog可读取日志信息') cmd = input('请输入指令:').strip() if cmd == 'readlog': with open(r'a.txt', mode='a+b') as f1: f1.write(bytes("\n{}".format(cmd), encoding='utf-8')) f1.seek(0, 0) res = f1.read().decode('utf-8') print(res) else: with open(r'a.txt', mode='ab') as f2: f2.write(bytes('\n{}'.format(cmd), encoding='utf-8'))