# coding:UTF-8
# @author:lsj
# @version:1.0
#1、通用文件copy工具实现
# src_file = input("请输入源文件路径:").strip()
# des_file = input("请输入目标文件路径:").strip()
# with open(r"{}".format(src_file),mode="rb") as f1,
# open(r"{}".format(des_file),mode='wb') as f2:
# for line in f1:
# f2.write(line)
#2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容
# 前提:创建一个a.txt文件,里面的内容是"abc你们好"
# 测试r+
# with open("a.txt",mode="r+",encoding="utf-8") as f:
# print(f.tell()) # 0 指针在文件开头
# print(f.read()) # abc你们好 文件全部内容
# # res = f.seek(0,0) # 0 指针在文件开头
# res = f.seek(0,1) # 0 指针在文件开头
# print(res)
# print(f.tell()) # 9
# 测试w+
# with open("a.txt",mode="w+",encoding="utf-8") as f:
# print(f.read()) # io.UnsupportedOperation: not readable不支持读
# f.write("abc你好")
# f.seek(2,0)
# print(f.tell()) # 2
# print(f.seek(2,0)) # 2
# f.seek(2,1) #报错 io.UnsupportedOperation: can't do nonzero cur-relative seeks
# print(f.tell())
# f.seek(2,2) # 报错:io.UnsupportedOperation: can't do nonzero end-relative seeks
# 测试a+
# with open("a.txt",mode="a+",encoding="utf-8") as f:
# f.read()
# print(f.read()) #
# f.write("abc你好")
# f.seek(2,0) # # 2
# print(f.tell())
# f.seek(2,1) #报错 io.UnsupportedOperation: can't do nonzero cur-relative seeks
# print(f.tell())
# f.seek(2,2) # 报错
# # 3、tail -f access.log程序实现 # 永远获取日志的最后一行。
# with open('access.log',mode='rb') as f:
# f.seek(0,2)
# while True:
# line=f.readlines()
# if len(line) == 0:
# pass
# else:
# print(line.decode('utf-8'),end='')
#4、周末作业参考在老师讲解完毕后(下午17:30开始讲解),练习熟练,明早日考就靠他俩
# 4.1:编写用户登录接口
# username = input("请输入您的账号:").strip()
# password = input("请输入密码:").strip()
# with open(r'user.txt',mode='rt',encoding="utf-8") as f:
# for line in f:
# user,pwd = line.strip().split(":")
# if user == username and pwd == password:
# print("登录成功!!")
# break
# else:
# print("登录失败!!!")
# 4.2:编写程序实现用户注册后(注册到文件中),可以登录(登录信息来自于文件)
# username = input("请输入您的注册账号:").strip()
# password = input("请输入您的注册密码:").strip()
# with open("login.txt",mode="wt",encoding="utf-8") as w:
# w.write("{}:{}
".format({username},{password}))
dic = {
"0":"退出",
"1":"登录",
"2":"注册"
}
while True:
msg = """
0 退出
1 登录
2 注册
"""
print(msg)
cmd = input('请输入命令编号>>: ').strip()
if not cmd.isdigit():
print('必须输入命令编号的数字,傻叉')
continue
# 判断当前用户如果不在dic字典中,执行
if cmd not in dic:
print('输入的命令不存在')
continue
if cmd == "0":
print("退出")
break
if cmd == "1":
username = input("请输入您的账号:").strip()
password = input("请输入密码:").strip()
with open('login.txt',mode='rt',encoding="utf-8") as f:
for line in f:
user,pwd = line.strip().split(":")
if user == username and pwd == password:
print("登录成功!!")
break
else:
print("登录失败!!!")
if cmd == "2":
username = input("请输入您的注册账号:").strip()
password = input("请输入您的注册密码:").strip()
with open("login.txt",mode="at",encoding="utf-8") as w:
w.write("{}:{}
".format(username,password))
print("注册成功可以登录!!!")
print(dic.get(cmd))
0317作业讲解
import os # os: 用于与操作系统交互的模块
# 1、通用文件copy工具实现
'''
while True:
# 1)接收用户需要拷贝的源文件
source_file = input('请输入源文件路径: ').strip()
# 2) 判断源文件是否存在
print(os.path.exists(source_file))
# os.path.exists(文件路径) ---> 返回的结果如果是True证明文件存在,否则不存在
# 若文件不存在,则让用户重新输入源文件路径
if not os.path.exists(source_file):
print('当前源文件路径不存在,请重新输入!')
continue
# 3) 让用户输入拷贝后的文件路径
dst_file = input('请输入拷贝后的文件路径: ').strip()
with open(source_file, 'rb') as f_r, open(dst_file, 'wb') as f_w:
for line in f_r:
f_w.write(line)
break
'''
# 2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容
# r+: 可读可写
# with open('test.txt', 'r+', encoding='utf-8') as f:
# # 光标从文件文本开头,从左到右移动6个字节
# f.seek(6)
# # 读取: 4个字符
# # res = f.read(4)
# # print(res)
#
# # 写入: alex
# f.write('alex')
# w+: 可读可写,每次打开都会覆盖原来的内容,无法读取文件中的内荣。
# with open('test.txt', 'w+', encoding='utf-8') as f:
# res = f.read()
# print(res)
# a+: 可读可写,无论是a或者是a+模块,追加时都会从末尾追加,指针移动无效
# with open('test.txt', 'a+', encoding='utf-8') as f:
# # 指针移动后
# f.seek(6)
# # 读取4个字符
# res = f.read(4)
# print(res)
# # 只能在文件文本中的末尾追加
# f.write('tank_is_very_handsome!')
# 3、tail -f access.log程序实现
# tail -f access.log: 监听access.log文件末尾是否有追加的内容
'''
import time
# 1) 读取文件的内容,循环监听
with open('access.log', 'rb') as f:
# 将指针移动到文件的最后一行的开头
f.seek(0, 2)
while True:
line = f.readline() # 读取一行数据
# 如果最后一行有数据则打印,否则继续监听
if line:
print(line.decode('utf-8'))
else:
# 否则等待,监听, 模拟延时操作
time.sleep(0.2) # 相当于每0.2秒监听一次文件末尾是否追加了数据
'''
# 2、编写tail工具
# 函数定义
"""
def tail_util(log_path):
'''
:param log_path: 用于接收,需要监听的日志文件
:return:
'''
import time
with open(log_path, 'rb') as f:
f.seek(0, 2)
while True:
line = f.readline()
if line:
print(line.decode('utf-8'))
else:
time.sleep(0.2)
# 函数调用
# tail_util('access.log')
"""
# 4、周末作业参考在老师讲解完毕后(下午17:30开始讲解),练习熟练,明早日考就靠他俩
# 4.1:编写用户登录接口
# 1)讲用户所有的数据读取出来,存放在字典中
'''
user_info = {}
with open('user_info.txt', mode='r', encoding='utf-8') as f:
for line in f:
user, pwd = line.strip().split(':')
# user_info ---> {'tank': '123'}
user_info[user] = pwd
print('所有的用户数据为: ', user_info)
while True:
username = input('请输入用户名: ').strip()
# 2) 校验用户名是否存在
# 若 用户名不存在user_info字典中,则表示当前用户不存在
if username not in user_info:
print('当前用户不存在,请重新输入!')
continue
password = input('请输入密码: ').strip()
if password == user_info.get(username):
print('登录成功!')
break
else:
print('登录失败!')
'''
# 4.2:编写程序实现用户注册后(注册到文件中),可以登录(登录信息来自于文件)
'''
# 1) 读取文件中所有用户的数据
user_info = {}
with open('user_info.txt', mode='r', encoding='utf-8') as f:
for line in f:
user, pwd = line.strip().split(':')
# user_info ---> {'tank': '123'}
user_info[user] = pwd
print('所有的用户数据为: ', user_info)
while True:
# 2) 让用户输入用户名与密码,进行注册
username = input('请输入注册的用户名: ').strip()
# 3) 判断当前用户是否存在,若存在则让其重新输入
if username in user_info: # True, 证明用户已存在,让用户重新输入
print('当前用户已存在,请重新输入!')
continue
# 4) 若用户不存在,则继续注册
password = input('请输入密码: ').strip()
re_password = input('请确认密码: ').strip()
# 5)判断用户输入两次密码是否一致
if password == re_password:
print('注册成功!')
# 6) 将注册成功的用户数据,追加到user_info.txt文件中
with open('user_info.txt', 'a', encoding='utf-8') as f:
f.write(f'{username}:{password}
')
break
else:
print('两次密码不一致!')
'''