My knowledge of how file buffers work is fairly weak, so forgive the simple mindedness of this question. I have a piece of code in Python that waits for a file to appear, then reads it.
while 1:
try:
f = open('file.txt')
f.close()
print "Message received."
break
except:
time.sleep(0.3)
Where file.txt
is a file written by another program. However, I've a suspicion that Python is reading the file and then closing the handle before the file file.txt
is completely written (that is, before I even call close()
in the program that is writing the file). Should this even be possible?
If it is the case, is there a way I can detect (in the reading program listed above) whether or not the buffers have been flushed before I call f.close()
?