def atomic_replace(src_filename, dst_filename):
 def same_fs(filename_a, filename_b):
  '\n        Checks if both files reside on the\n        same FS device\n        '
  stats_a = os.stat(filename_a)
  stats_b = os.stat(filename_b)
  return (stats_a.st_dev == stats_b.st_dev)
 if (os.path.exists(dst_filename) and (not same_fs(src_filename, dst_filename))):
  dst_path = os.path.dirname(os.path.abspath(dst_filename))
  dst_temp_filename = os.tempnam(dst_path)
  shutil.copy(src_filename, dst_temp_filename)
  shutil.move(dst_temp_filename, dst_filename)
 else:
  shutil.move(src_filename, dst_filename)

def atomic_replace ( src_filename , dst_filename ) : def same_fs ( filename_a , filename_b ) : stats_a = os . stat ( filename_a ) stats_b = os . stat ( filename_b ) return ( stats_a . st_dev == stats_b . st_dev ) if ( os . path . exists ( dst_filename ) and ( not same_fs ( src_filename , dst_filename ) ) ) : dst_path = os . path . dirname ( os . path . abspath ( dst_filename ) ) dst_temp_filename = os . tempnam ( dst_path ) shutil . copy ( src_filename , dst_temp_filename ) shutil . move ( dst_temp_filename , dst_filename ) else : shutil . move ( src_filename , dst_filename )
does an "atomic" replace of a file by another .