Sunday, November 7, 2010

Writing a File Atomically from Python

I wanted to be able to write to a file, but there was a possibility that others could be reading from it while I was writing to it. So I came up with this code below (which I release as public domain) which should guarantee that the file gets written without anyone being able to read it until it is completely written to disk.

import os, os.path
import errno

def write(filename, data, mode=0600):
""" guarantees that noone can read filename with only partial data """
fd = None
seq = 1
dn = os.path.dirname(filename)
bn = os.path.basename(filename)
while fd == None:
try:
fn = os.path.join(dn,".%s.%d"%(bn,seq))
fd = os.open(fn, os.O_CREAT | os.O_EXCL | os.O_WRONLY, mode)
except OSError, e:
if e.errno == errno.EEXIST:
seq += 1
continue
else:
raise

# write data to temporary file
fo = os.fdopen(fd, "w")
fo.write(str(data))
fo.close
#os.close(fd)

# move file into place now that it is written
os.rename(fn, filename)

No comments: