tags:

views:

54

answers:

3

If I have a script that writes 1000 lines to file and then continues regular expressions against that file, however only the last 100 lines of text written will be available. One way around this is to close and reopen the file. Is there a way to reload a file after being written to or should I just make a write close open module? It may be relevant that the logfile did not exist/is empty on the first open.

>>> the_page = 'some large site opened through urllib'
>>> logfile = open('./artist/' + tags['ARTIST'], 'r+') 
>>> logfile.write(the_page)
>>> print logfile.read()

Nothing appears.

>>> 'Some regular expressions search'

Searches against last 100 lines written.

>>> logfile.close()
>>> logfile = open('./artist/' + tags['ARTIST'], 'r+') 
>>> print logfile.read()

Everything appears.

>>> 'Some regular expressions search'

Performs as expected.

A: 
logfile.flush()

This should do the same thing as opening then closing the file.

Yeah, I pretty much want to do the opposite of this. Flushing is interchangeable with closing as far as my problem is concerned.
teratomata
@teratomata, can you clarify why you don't want to flush the file?
gnibbler
Flushing clears the file object, what I wanted to do is reload the file that has been written to. Actually what I didn't know I wanted to do was go back to the beginning of the file, which seek(0) does.
teratomata
A: 

As the comments point out, this is the wrong way to go about this. seek(0) is the right way.

This had quite an obvious answer, I don't need to close the file or anything, I can just open it again and it acts the same as a reload would. So:

>>> the_page = 'some large site opened through urllib'
>>> logfile = open('./artist/' + tags['ARTIST'], 'r+') 
>>> logfile.write(the_page)
>>> logfile = open('./artist/' + tags['ARTIST'], 'r+') 
>>> print logfile.read()
teratomata
That most probably closed the file and then reopened it.As Space_C0wb0y wrote, you should probably seek(0), because after writing you are positioned at the end of the file and that is why you can't read anything.
zvonimir
With cpython's refcounting, this is just doing a hidden `flush` and `close` of the file when you reopen the file. This won't work properly for any implementation of Python that uses another type of garbarge collection
gnibbler
You're right, I don't know why I didn't realize this.
teratomata
+1  A: 

This is just a hunch, but maybe a logfile.seek(0) after the writing is finished may help. A possible explanation is given here (last scentence).

Space_C0wb0y