Hi list. I'm writing a tail -f like program in python and I found file.read() doesn't work as I think it should.
Here's the code illustrating my problem. ### #!/usr/bin/env python import os, sys filename = "test.out" f = open(filename, "w+") f.write("Hello") f.flush() f.seek(0, 2) statinfo = os.stat(filename) print "file size: %d" % statinfo.st_size print "position : %d" % f.tell() line = f.read() print "line : [%s]" % line # Writing the same file using another fd f2 = open(filename, "a+") f2.write("World") f2.flush() f2.close() statinfo = os.stat(filename) print "file size: %d" % statinfo.st_size print "position : %d" % f.tell() line = f.read() # read() returns emtpy!! readlines?() works ok ### Running the above, I got the following. ### file size: 5 position : 5 line : [] file size: 10 position : 5 ### So my question is why the second f.read() returns an emtpy? >From tell() and its st_size I'm sure that file descriptor is not at the EOF and read()'s doc says "An empty string is returned when EOF is encountered immediately". Using readline() or readlines() instead of read() works great though. I'm using Python 2.4.3 on OS X. Probably I'm missing something but I could't figure out. Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list