Python 之读取大文件readline与readlines的差别
Posted yang-2018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 之读取大文件readline与readlines的差别相关的知识,希望对你有一定的参考价值。
import time def get_all_lines(filename): start_time = time.time() try: f = open(filename, ‘rb‘) except Exception as e: exit(e) f.readlines() f.close() end_time = time.time() - start_time print("get_all_lines:%s" % end_time) def get_one_line(filename): start_time = time.time() try: f = open(filename, ‘rb‘) except Exception as e: exit(e) while True: string = f.readline() if not string: break f.close() end_time = time.time() - start_time print("get_one_line:%s" % end_time) if __name__ == ‘__main__‘: file_name = "des114.sql" get_all_lines(file_name) # 23.311237573623657 get_one_line(file_name) # 2.696101665496826
以上是关于Python 之读取大文件readline与readlines的差别的主要内容,如果未能解决你的问题,请参考以下文章