def replace_file(file_name, data):
 base_dir = os.path.dirname(os.path.abspath(file_name))
 tmp_file = tempfile.NamedTemporaryFile('w+', dir=base_dir, delete=False)
 tmp_file.write(data)
 tmp_file.close()
 os.chmod(tmp_file.name, 420)
 os.rename(tmp_file.name, file_name)

def replace_file ( file_name , data ) : base_dir = os . path . dirname ( os . path . abspath ( file_name ) ) tmp_file = tempfile . NamedTemporaryFile ( 'w+' , dir = base_dir , delete = False ) tmp_file . write ( data ) tmp_file . close ( ) os . chmod ( tmp_file . name , 420 ) os . rename ( tmp_file . name , file_name )
replaces the contents of file_name with data in a safe manner .