文件处理作业
# #一:今日作业:
tail -f access.log监测日志小程序
写入文件名
with open("access.log",mode="a",encoding="utf8")as f:
f.write("你好呀")
2.
import time with open('access.log', mode="rb") as f: #可以定制函数写活代码 f.seek(0,2) while True: #记住循环while 和打开文件的单词 res=f.readline() if len(res) == 0: time.sleep(0.3) else: print(res.decode("utf-8"),end="") #输出格式可以不要引号
# #1、编写文件copy工具
name=input("qinfhus") age=input("yaobaobeidedizhi") #这是清空后写入 with open(r"{}".format(name),mode="r",encoding="utf8")as f_1,\ open(r"{}".format(age),mode="w",encoding="utf8")as f_2: res=f_1.read() f_2.write(res)
)
# #2、编写登录程序,账号密码来自于文件
name=input("qinfhus") age=input("yaobaobeidedizhi") #这是清空后写入 with open('D:\pythonyanshi\作业考试\账号密码', mode='rt', encoding='utf-8') as f_1: for i in f_1: name_1,age_1=i.strip().split(":") if name_1==name and age_1==age: print("登录成功") else: print("密码错误")
# 3、编写注册程序,账号密码来存入文件
name=input("qinfhus") age=input("yaobaobeidedizhi") #这是清空后写入 with open('D:\pythonyanshi\作业考试\账号密码', mode='w', encoding='utf-8') as f_1: f_1.write("{}:{}".format(name,age)) #这是一个整体"{}:{}"
1、通用文件copy工具实现
name=input("qinfhus") age=input("yaobaobeidedizhi") #这是清空后写入 with open(r"{}".format(name),mode="rb")as f_1,\ open(r"{}".format(age),mode="ab")as f_2: while True: res=f_1.read(1024) 也可以使用for循环 f_2.write(res) if len(res)==0: break
# 2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容
# 2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容 # with open("a.txt","r+",encoding="utf-8") as f : #r+ # f.read() #光标在末尾 # print(f.tell()) #查看光标所在位置 # f.seek(3,0) #把光标定位在前三个位置 只能使用0 # print(f.tell()) # f.write("123") #打印直接覆盖 # 记得字符串添加 # with open("a.txt","w+",encoding="utf-8") as f : #w+ # f.read() #光标在最开始为然后 # print(f.tell()) #查看光标所在位置 # f.seek(3,0) #把光标定位在前三个位置 # print(f.tell()) # f.write("123") #直接打印打印 # 记得字符串添加 with open("a.txt","a+",encoding="utf-8") as f : #a+ f.read() #光标在末尾 print(f.tell()) #查看光标所在位置 f.seek(3,0) #把光标定位在前三个位置 print(f.tell()) f.write("123") #打印直接向后添加 文件末尾读取 # 记得字符串添加
# 4.1:编写用户登录接口
name=input("qinfhus") age=input("yaobaobeidedizhi") #这是清空后写入 with open('D:\pythonyanshi\作业考试\账号密码', mode='rt', encoding='utf-8') as f_1: for i in f_1: name_1,age_1=i.strip().split(":") if name_1==name and age_1==age: print("登录成功") else: print("密码错误")
# 4.2:编写程序实现用户注册后(注册到文件中),可以登录(登录信息来自于文件)