对于小文件,可以用readlines()
f = open('start.log', 'r')for line in f.readlines(): do_sth(line)
但是对于好几G的巨型文本文件,这样就无法全部载入到内存, 可以使用下面的方法
with open("log.txt") as infile: for line in infile: do_something_with(line)
参考资料
http://stackoverflow.com/questions/519633/lazy-method-for-reading-big-file-in-python
http://stackoverflow.com/questions/8009882/how-to-read-large-file-line-by-line-in-python
原文地址:python逐行读入大文件, 感谢原作者分享。 十七岁怎么会有七十岁的忧伤,十八岁怎么会有八十岁的等待。