Python 撤消文件 readline() 操作,使文件指针恢复到原始状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3505479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undo a file readline() operation so file-pointer is back in original state
提问by MikeN
I'm browsing through a Python file pointer of a text file in read-only mode using file.readline() looking for a special line. Once I find that line I want to pass the file pointer to a method that is expecting the file pointer to be at the START of that readline (not right after it.)
我正在使用 file.readline() 以只读模式浏览文本文件的 Python 文件指针以查找特殊行。一旦我找到那行,我想将文件指针传递给一个方法,该方法期望文件指针位于该 readline 的 START 处(而不是在它之后。)
How do I essentially undo one file.readline() operation on a file pointer?
我如何从本质上撤消文件指针上的一个 file.readline() 操作?
采纳答案by D.Shawley
You have to remember the position by calling file.tell()before the readline and then calling file.seek()to rewind. Something like:
您必须通过file.tell()在 readline 之前调用然后调用file.seek()倒带来记住位置。就像是:
fp = open('myfile')
last_pos = fp.tell()
line = fp.readline()
while line != '':
if line == 'SPECIAL':
fp.seek(last_pos)
other_function(fp)
break
last_pos = fp.tell()
line = fp.readline()
I can't recall if it is safe to call file.seek()inside of a for line in fileloop so I usually just write out the whileloop. There is probably a much more pythonic way of doing this.
我不记得在循环file.seek()内部调用是否安全,for line in file所以我通常只写出while循环。可能有一种更pythonic的方式来做到这一点。
回答by Alex Martelli
You record the starting point of the line with thefile.tell()before you call readline, and get back to that point, if you need to, with thefile.seek.
thefile.tell()在调用之前用 记录线的readline起点,如果需要,用 返回到该点thefile.seek。
>>> with open('bah.txt', 'w') as f:
... f.writelines('Hello %s\n' % i for i in range(5))
...
>>> with open('bah.txt') as f:
... f.readline()
... x = f.tell()
... f.readline()
... f.seek(x)
... f.readline()
...
'Hello 0\n'
'Hello 1\n'
'Hello 1\n'
>>>
as you see, the seek/tell "pair" is "undoing", so to speak, the file pointer movement performed by readline. Of course, this can only work on an actual seekable (i.e., disk) file, not (e.g.) on file-like objects built w/the makefile method of sockets, etc etc.
如您所见,seek/tell“对”正在“撤消”,可以说,文件指针移动由readline. 当然,这仅适用于实际的可查找(即磁盘)文件,而不适用于(例如)使用套接字的 makefile 方法等构建的类文件对象。
回答by unutbu
If your method simply wants to iterate through the file, then you could use itertools.chainto make an appropriate iterator:
如果你的方法只是想遍历文件,那么你可以使用itertools.chain一个合适的迭代器:
import itertools
def process(it):
for line in it:
print line,
with open(filename,'r') as f:
for line in f:
if 'marker' in line:
it=itertools.chain((line,),f)
process(it)
break
回答by GWW
fin = open('myfile')
for l in fin:
if l == 'myspecialline':
# Move the pointer back to the beginning of this line
fin.seek(fin.tell() - len(l))
break
# now fin points to the start of your special line
回答by JLT
If you don't know the last line because you didn't visit it you can read backwards until you see a newline character:
如果您不知道最后一行,因为您没有访问它,您可以向后阅读,直到看到换行符:
with open(logfile, 'r') as f:
# go to EOF
f.seek(0, os.SEEK_END)
nlines = f.tell()
i=0
while True:
f.seek(nlines-i)
char = f.read(1)
if char=='\n':
break
i+=1

