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)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM