代码如下:fp.read()中限定了字节数目,然后以一个循环统计出整体的行数。

 

def lines_tj(filename):
 count = 0
 with open(filename, "r", encoding='utf-8') as fp:
   while 1:
     buffer = fp.read(8*1024*1024)
     if not buffer:
       break
     count += buffer.count('\n')
   return count