Python 为什么我不能在打开的文件上调用 read() 两次?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3906137/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:17:47  来源:igfitidea点击:

Why can't I call read() twice on an open file?

pythonio

提问by helpermethod

For an exercise I'm doing, I'm trying to read the contents of a given file twice using the read()method. Strangely, when I call it the second time, it doesn't seem to return the file content as a string?

对于我正在做的练习,我尝试使用该read()方法两次读取给定文件的内容。奇怪的是,当我第二次调用它时,它似乎没有将文件内容作为字符串返回?

Here's the code

这是代码

f = f.open()

# get the year
match = re.search(r'Popularity in (\d+)', f.read())

if match:
  print match.group(1)

# get all the names
matches = re.findall(r'<td>(\d+)</td><td>(\w+)</td><td>(\w+)</td>', f.read())

if matches:
  # matches is always None

Of course I know that this is not the most efficient or best way, this is not the point here. The point is, why can't I call read()twice? Do I have to reset the file handle? Or close / reopen the file in order to do that?

当然我知道这不是最有效或最好的方式,这不是重点。关键是,为什么我不能打电话read()两次?我必须重置文件句柄吗?或者关闭/重新打开文件才能做到这一点?

采纳答案by Tim

Calling read()reads through the entire file and leaves the read cursor at the end of the file (with nothing more to read). If you are looking to read a certain number of lines at a time you could use readline(), readlines()or iterate through lines with for line in handle:.

调用read()读取整个文件并将读取光标留在文件末尾(没有更多可读取的内容)。如果您希望一次读取一定数量的行,您可以使用 readline()readlines()或者使用for line in handle:.

To answer your question directly, once a file has been read, with read()you can use seek(0)to return the read cursor to the start of the file (docs are here). If you know the file isn't going to be too large, you can also save the read()output to a variable, using it in your findall expressions.

要直接回答您的问题,一旦读取了文件,read()您可以使用seek(0)将读取光标返回到文件的开头(文档在此处)。如果您知道文件不会太大,您还可以将read()输出保存到变量中,在 findall 表达式中使用它。

Ps. Dont forget to close the file after you are done with it ;)

附言。完成后不要忘记关闭文件;)

回答by Ignacio Vazquez-Abrams

The read pointer moves to after the last read byte/character. Use the seek()method to rewind the read pointer to the beginning.

读指针移到最后一个读字节/字符之后。使用该seek()方法将读取指针倒回到开头。

回答by Douglas Leeder

Every open file has an associated position.
When you read() you read from that position. For example read(10)reads the first 10 bytes from a newly opened file, then another read(10)reads the next 10 bytes. read()without arguments reads all of the contents of the file, leaving the file position at the end of the file. Next time you call read()there is nothing to read.

每个打开的文件都有一个相关的位置。
当您 read() 时,您从该位置读取。例如read(10),从新打开的文件中读取前 10 个字节,然后另一个read(10)读取接下来的 10 个字节。 read()不带参数读取文件的所有内容,将文件位置留在文件末尾。下次你打电话时read(),没有什么可读的。

You can use seekto move the file position. Or probably better in your case would be to do one read()and keep the result for both searches.

您可以使用seek移动文件位置。或者在您的情况下可能更好的是做一个read()并保留两个搜索的结果。

回答by Tom Anderson

Everyone who has answered this question so far is absolutely right - read()moves through the file, so after you've called it, you can't call it again.

到目前为止回答这个问题的每个人都是绝对正确的 -read()在文件中移动,所以在你调用它之后,你不能再次调用它。

What I'll add is that in your particular case, you don't need to seek back to the start or reopen the file, you can just store the text that you've read in a local variable, and use it twice, or as many times as you like, in your program:

我要补充的是,在您的特定情况下,您无需返回开始或重新打开文件,您只需将已阅读的文本存储在局部变量中,然后使用它两次,或者在你的程序中,你喜欢多少次:

f = f.open()
text = f.read() # read the file into a local variable
# get the year
match = re.search(r'Popularity in (\d+)', text)
if match:
  print match.group(1)
# get all the names
matches = re.findall(r'<td>(\d+)</td><td>(\w+)</td><td>(\w+)</td>', text)
if matches:
  # matches will now not always be None

回答by towi

read()consumes. So, you could resetthe file, or seekto the start before re-reading. Or, if it suites your task, you can use read(n)to consume only nbytes.

read()消耗. 因此,您可以重置文件,或在重新阅读之前寻找开始。或者,如果它read(n)适合您的任务,您可以只使用n字节。

回答by Ant

yeah, as above...

是的,如上...

i'll write just an example:

我只写一个例子:

>>> a = open('file.txt')
>>> a.read()
#output
>>> a.seek(0)
>>> a.read()
#same output

回答by whatnick

I always find the read method something of a walk down a dark alley. You go down a bit and stop but if you are not counting your steps you are not sure how far along you are. Seek gives the solution by repositioning, the other option is Tell which returns the position along the file. May be the Python file api can combine read and seek into a read_from(position,bytes) to make it simpler - till that happens you should read this page.

我总是觉得阅读方法有点像走在黑暗的小巷里。你往下走一点然后停下来,但如果你不计算你的步数,你就不确定你走了多远。Seek 通过重新定位给出解决方案,另一个选项是 Tell,它返回沿文件的位置。可能是 Python 文件 api 可以将 read 和 seek 结合到 read_from(position,bytes) 中以使其更简单 - 在发生这种情况之前,您应该阅读此页面