Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

io

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?

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?

like image 291
helpermethod Avatar asked Oct 11 '10 12:10

helpermethod


People also ask

How do I read the same file twice in Python?

You need to refill the source with 'data' and then you can work with the same data again.

What does open () read () do in Python?

Python has a built-in open() function to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. We can specify the mode while opening a file. In mode, we specify whether we want to read r , write w or append a to the file.

What happens when we use the read () method to access a file?

read() : This function reads the entire file and returns a string. readline() : This function reads lines from that file and returns as a string.

How do I open file in read mode and append?

You're looking for the r+ or a+ mode, which allows read and write operations to files (see more). With r+ , the position is initially at the beginning, but reading it once will push it towards the end, allowing you to append. With a+ , the position is initially at the end.


1 Answers

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:.

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.

Ps. Don't forget to close the file after you are done with it.

like image 61
Tim Avatar answered Sep 24 '22 22:09

Tim