Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么可以';我不能对打开的文件调用read()两次吗?_Python_Io - Fatal编程技术网

Python 为什么可以';我不能对打开的文件调用read()两次吗?

Python 为什么可以';我不能对打开的文件调用read()两次吗?,python,io,Python,Io,对于我正在做的一个练习,我尝试使用read()方法读取给定文件的内容两次。奇怪的是,当我第二次调用它时,它似乎没有以字符串形式返回文件内容 这是密码 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+)&

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

这是密码

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
f=f.open()
#过年
match=re.search(r'Popularity in(\d+),f.read())
如果匹配:
打印匹配组(1)
#得到所有的名字
matches=re.findall(r'(\d+)(\w+)(\w+),f.read()
如果匹配:
#火柴总是没有
当然,我知道这不是最有效或最好的方法,这不是重点。问题是,为什么我不能调用
read()
两次呢?我必须重置文件句柄吗?或者关闭/重新打开文件以执行此操作?

调用
read()
读取整个文件,并将读取光标保留在文件末尾(没有更多内容可读取)。如果希望一次读取一定数量的行,可以使用
readline()
readlines()
,或使用
对句柄中的行进行迭代:

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

注意:完成后不要忘记关闭文件;)

调用
read()。如果希望一次读取一定数量的行,可以使用
readline()
readlines()
,或使用
对句柄中的行进行迭代:

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


注意:完成后不要忘记关闭文件;)

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

读取指针移动到上次读取的字节/字符之后。使用
seek()
方法将读取指针倒回起始位置。

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


您可以使用
seek
移动文件位置。或者在您的情况下,最好执行一次
read()
,并保留两次搜索的结果。

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


您可以使用
seek
移动文件位置。或者在您的情况下,最好执行一次
read()
,并保留两次搜索的结果。

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

我要补充的是,在您的特定情况下,您不需要返回到起始位置或重新打开文件,您只需将读取的文本存储在局部变量中,然后在程序中使用它两次或任意多次:

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
f=f.open()
text=f.read()#将文件读入局部变量
#过年
匹配=重新搜索(r'Popularity in(\d+),文本)
如果匹配:
打印匹配组(1)
#得到所有的名字
matches=re.findall(r'(\d+)(\w+)(\w+),text)
如果匹配:
#匹配现在并不总是无

到目前为止,所有回答这个问题的人都是绝对正确的-
read()
将遍历该文件,因此在调用该文件后,您不能再次调用它

我要补充的是,在您的特定情况下,您不需要返回到起始位置或重新打开文件,您只需将读取的文本存储在局部变量中,然后在程序中使用它两次或任意多次:

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
f=f.open()
text=f.read()#将文件读入局部变量
#过年
匹配=重新搜索(r'Popularity in(\d+),文本)
如果匹配:
打印匹配组(1)
#得到所有的名字
matches=re.findall(r'(\d+)(\w+)(\w+),text)
如果匹配:
#匹配现在并不总是无
read()
消耗。因此,您可以重置该文件,或在重新读取之前查找到起始位置。或者,如果它适合您的任务,您可以使用
read(n)
仅消耗
n
字节。

read()
消耗。因此,您可以重置该文件,或在重新读取之前查找到起始位置。或者,如果它适合您的任务,您可以使用
read(n)
仅消耗
n
字节。

是的,如上所述

我只写一个例子:

>>> a = open('file.txt')
>>> a.read()
#output
>>> a.seek(0)
>>> a.read()
#same output
是的,如上所述

我只写一个例子:

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

我总是觉得阅读的方法有点像走在黑暗的小巷里。你往下走了一点,然后停了下来,但如果你不数数你的脚步,你就不知道你走了多远。Seek通过重新定位给出解决方案,另一个选项是告诉您是哪一个re