def safewrite(filename, content):
 f = file((filename + '.tmp'), 'w')
 f.write(content)
 f.close()
 os.rename(f.name, filename)

def safewrite ( filename , content ) : f = file ( ( filename + '.tmp' ) , 'w' ) f . write ( content ) f . close ( ) os . rename ( f . name , filename )
writes the content to a temp file and then moves the temp file to given filename to avoid overwriting the existing file in case of errors .