I need to get a line count of a large file (hundreds of thousands of lines) in python.
(我需要在python中获取一个大文件(数十万行)的行数。)
What is the most efficient way both memory- and time-wise? (内存和时间最有效的方法是什么?)
At the moment I do:
(目前,我这样做:)
def file_len(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
is it possible to do any better?
(有可能做得更好吗?)
ask by SilentGhost translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…