Python 提取很长txt文件的最后一行
我有一个包含数据的很长的文件(“text.txt”),还有一个文件正好包含一行,这是text.txt的最后一行。当text.txt每10分钟收到另一行时,这一行应该每10分钟覆盖一次(通过简单的计时作业完成) 因此,根据我在stackoverflow上找到的其他代码片段,我目前运行以下代码:Python 提取很长txt文件的最后一行,python,python-2.7,Python,Python 2.7,我有一个包含数据的很长的文件(“text.txt”),还有一个文件正好包含一行,这是text.txt的最后一行。当text.txt每10分钟收到另一行时,这一行应该每10分钟覆盖一次(通过简单的计时作业完成) 因此,根据我在stackoverflow上找到的其他代码片段,我目前运行以下代码: #!/usr/bin/env python import os, sys file = open(sys.argv[1], "r+") #Move the pointer (similar to a
#!/usr/bin/env python
import os, sys
file = open(sys.argv[1], "r+")
#Move the pointer (similar to a cursor in a text editor) to the end of the file.
file.seek(0, os.SEEK_END)
#This code means the following code skips the very last character in the file -
#i.e. in the case the last line is null we delete the last line
#and the penultimate one
pos = file.tell() - 1
#Read each character in the file one at a time from the penultimate
#character going backwards, searching for a newline character
#If we find a new line, exit the search
while pos > 0 and file.read(1) != "\n":
pos -= 1
file.seek(pos, os.SEEK_SET)
#So long as we're not at the start of the file, delete all the characters ahead of this position
if pos > 0:
file.seek(pos, os.SEEK_SET)
w = open("new.txt",'w')
file.writelines(pos)
w.close()
file.close()
使用此代码,我得到了错误:
TypeError:writelines()需要一个iterable参数
(当然)。使用file.truncate()
时,我可以去掉原始文件中的最后一行;但我想把它保留在那里,然后把最后一行提取到new.txt。但我不明白在使用file.seek时这是如何工作的。所以我需要代码最后一部分的帮助
file.readlines()
带有行[:-1]
的文件无法正常处理如此大的文件。不知道为什么要打开w
,但却没有对其执行任何操作就将其关闭。如果希望new.txt
将文件
中的所有文本从pos
开始,并在末尾结束,那么:
if pos > 0:
file.seek(pos, os.SEEK_SET)
w = open("new.txt",'w')
w.write(file.read())
w.close()
根据您的代码,
pos
是一个整数,用于表示从文件末尾开始的第一个\n
的位置
无法执行-file.writelines(pos)
,因为writelines需要一个行列表。但是pos
是一个整数
您还希望写入new.txt
,因此应该使用w
文件来写入,而不是文件
。范例-
if pos > 0:
file.seek(pos, os.SEEK_SET)
w = open("new.txt",'w')
w.write(file.read())
w.close()
以下方法如何:
max_line_length = 1000
with open(sys.argv[1], "r") as f_long, open('new.txt', 'w') as f_new:
f_long.seek(-max_line_length, os.SEEK_END)
lines = [line for line in f_long.read().split("\n") if len(line)]
f_new.write(lines[-1])
这将查找到文件的几乎末尾,并在中读取文件的其余部分。然后将其拆分为非空行,最后一个条目将写入
new.txt
以下是如何将文件的最后两行尾随到列表中:
import subprocess
output = subprocess.check_output(['tail', '-n 2', '~/path/to/my_file.txt'])
lines = output.split('\n')
现在,您可以从列表中获取所需的信息
行
您考虑过只使用tail命令吗?如果您只想读而不想写,那么存储“当前”最后一行(或文件大小)的偏移量,并在下一次迭代中直接查找该位置如何?