tags:

views:

205

answers:

7

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?

+16  A: 

read() reads through the entire file - so the read cursor is now at the end (with nothing more to read)

If you are looking to read a certain amount of lines at a time you could use readline() or readlines()

To answer your question, once a file has been read, with read(), calling it again will not work - you can use seek() to return the read cursor to the start of the file. Or save the read() output to a variable, using it in your findall expressions.

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

Tim
+1, Yes, please read to temporary variable to avoid unnecessary file I/O. It's a false economy that you're saving any memory because you have fewer (explicit) variables.
Nick T
+7  A: 

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

Ignacio Vazquez-Abrams
+2  A: 

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.

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

Douglas Leeder
+7  A: 

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.

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
Tom Anderson
+1 Actually this was the proposed solution for this exercise (http://code.google.com/intl/de-DE/edu/languages/google-python-class/exercises/baby-names.html). But somehow I didn't thought of storing the string in a variable. D'oh!
Helper Method
+1  A: 

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

towi
+2  A: 

yeah, as above...

i'll write just an example:

>>> a = open('file.txt')
>>> a.read()
#output
>>> a.seek(0)
>>> a.read()
#same output
Ant
+1  A: 

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.

whatnick