safe simultaneous file append?
Aahz Maruch
aahz at panix.com
Tue Oct 9 22:48:16 EDT 2001
In article <mailman.1002521586.6848.python-list at python.org>,
Graham Guttocks <graham_guttocks at yahoo.co.nz> wrote:
>
>Given the following code, is there any danger of the logfile being
>corrupted if two copies of this code try to append at the same time?
>
> file = open('logfile.txt','a+')
> file.write('testing' + '\n')
> file.close()
As others have mentioned, you want to make sure it's a single write()
call. Instead of flush(), though, I recommend that you do
file = open('logfile.txt', 'a+', 0)
to force unbuffered mode.
(Of course, the close() should flush it, too, but I'd suggest leaving
the file open for speed.)
--
--- Aahz <*> (Copyright 2001 by aahz at pobox.com)
Hugs and backrubs -- I break Rule 6 http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het Pythonista
We must not let the evil of a few trample the freedoms of the many.
More information about the Python-list
mailing list