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