行只是由换行符char分隔的数据'\n'.
1)由于行是可变长度的,你必须读取整个文件以知道换行符的位置,这样你就可以计算多少行:
count = 0
for line in open('myfile'):
count += 1
print count, line # it will be the last line
Run Code Online (Sandbox Code Playgroud)
2)从文件末尾读取一个块是查找最后一个换行符char的最快方法.
def seek_newline_backwards(file_obj, eol_char='\n', buffer_size=200):
if not file_obj.tell(): return # already in beginning of file
# All lines end with \n, including the last one, so assuming we are just
# after one end of line char
file_obj.seek(-1, os.SEEK_CUR)
while file_obj.tell():
ammount = min(buffer_size, file_obj.tell())
file_obj.seek(-ammount, os.SEEK_CUR)
data = file_obj.read(ammount)
eol_pos = data.rfind(eol_char)
if eol_pos != -1:
file_obj.seek(eol_pos - len(data) + 1, os.SEEK_CUR)
break
file_obj.seek(-len(data), os.SEEK_CUR)
Run Code Online (Sandbox Code Playgroud)
您可以这样使用:
f = open('some_file.txt')
f.seek(0, os.SEEK_END)
seek_newline_backwards(f)
print f.tell(), repr(f.readline())
Run Code Online (Sandbox Code Playgroud)
我们不要忘记
f = open("myfile.txt")
lines = f.readlines()
numlines = len(lines)
lastline = lines[-1]
Run Code Online (Sandbox Code Playgroud)
注意:这将以内容的形式读取内存中的整个文件.在文件非常大的情况下请记住这一点.
在最简单的方法就是文件读入到内存中.例如:
f = open('filename.txt')
lines = f.readlines()
num_lines = len(lines)
last_line = lines[-1]
Run Code Online (Sandbox Code Playgroud)
但是对于大文件,这可能会占用大量内存,因为整个文件都会加载到RAM中.另一种方法是逐行遍历文件.例如:
f = open('filename.txt')
num_lines = sum(1 for line in f)
Run Code Online (Sandbox Code Playgroud)
这样效率更高,因为它不会将整个文件加载到内存中,而只能一次查看一行.如果你想要最后一行,你可以在迭代时跟踪这些行,并通过以下方式获得两个答案:
f = open('filename.txt')
count=0
last_line = None
for line in f:
num_lines += 1
last_line = line
print "There were %d lines. The last was: %s" % (num_lines, last_line)
Run Code Online (Sandbox Code Playgroud)
如果您只需要最后一行,最后一个可能的改进是从文件末尾开始,然后向后搜索,直到找到换行符. 这是一个有一些代码执行此操作的问题.如果你同时需要两个行数,那么除了迭代文件中的所有行之外别无选择.