Python-为什么我不能在打开的文件上两次调用read()?
发布于 2021-02-02 23:23:46
我正在尝试使用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
当然我知道这不是最有效或最好的方法,这不是重点。关键是,为什么我不能调用read()
两次?我需要重置文件句柄吗?或者关闭/重新打开文件以执行此操作?
关注者
0
被浏览
263
1 个回答