Python读取文件首行和最后一行

file = open('XXX','rb') 
  • 使用链接中第2种批量处理文件的最后一行
count = 0
for i in range(88,100):
    for j in range(0,30):
        if os.access('solution//charac_'+str(i)+'_'+str(j)+'.txt', os.F_OK): # 如果文件存在,则进行下一步操作
            file = open('solution//charac_'+str(i)+'_'+str(j)+'.txt','rb')  # 文件打开方式为'rb',否则seek函数会报错
            offset = -50
            while True:
                file.seek(offset, 2)
                lines = file.readlines()
                if len(lines) >= 2:
                    last_line = lines[-1]
                    break
                offset *= 2
            res = int(last_line[8:10])
            if res <= 79:
                count=count+1

print(count)
posted @ 2020-03-22 11:37  rookieveteran  阅读(1699)  评论(0编辑  收藏  举报