有效地找到文本文件中的最后一行 [英] Efficiently finding the last line in a text file

查看:50
本文介绍了有效地找到文本文件中的最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从许多非常大(数百兆字节)的文本文件中提取最后一行来获取某些数据.目前,我正在使用 python 循环遍历所有行,直到文件为空,然后处理返回的最后一行,但我确信有更有效的方法来做到这一点.

I need to extract the last line from a number of very large (several hundred megabyte) text files to get certain data. Currently, I am using python to cycle through all the lines until the file is empty and then I process the last line returned, but I am certain there is a more efficient way to do this.

使用 python 仅检索文本文件最后一行的最佳方法是什么?

What is the best way to retrieve just the last line of a text file using python?

推荐答案

不是直接的方法,但可能比简单的 Python 实现快得多:

Not the straight forward way, but probably much faster than a simple Python implementation:

line = subprocess.check_output(['tail', '-1', filename])

这篇关于有效地找到文本文件中的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆