#这个程序用途:往access.log文件末尾追加数据
with open(r'access.log',mode="ta",encoding="utf-8") as ta:
#a模式:打开文件指针直接跳到文件末尾
ta.write("hhhhhhhhhhh3\n")
#这个程序用途:读取access.log文件末尾追加数据
import time
with open(r'access.log',mode="br") as tr:
#1.将指针跳到文件末尾
#tr.read() #错误的思路,直接将硬盘数据读入内存,指针在文件末尾。这样导致内存溢出
tr.seek(0,2) #指针的移动都是以bytes为单位,所以打开文件使用binary
#print(tr.tell())
while True:
res_line=tr.readline()
if len(res_line) == 0:
time.sleep(0.5)
#continue :注意这一块不需要continue,当读取一行的长度为0时,就重新进行while循环了
else:
print(res_line.decode("utf-8"),end="")