def file_move_safe(old_file_name, new_file_name, chunk_size=(1024 * 64), allow_overwrite=False):
 if _samefile(old_file_name, new_file_name):
  return
 try:
  os.rename(old_file_name, new_file_name)
  return
 except OSError:
  pass
 old_file = open(old_file_name, 'rb')
 try:
  fd = os.open(new_file_name, (((os.O_WRONLY | os.O_CREAT) | getattr(os, 'O_BINARY', 0)) | (((not allow_overwrite) and os.O_EXCL) or 0)))
  try:
   locks.lock(fd, locks.LOCK_EX)
   current_chunk = None
   while (current_chunk != ''):
    current_chunk = old_file.read(chunk_size)
    os.write(fd, current_chunk)
  finally:
   locks.unlock(fd)
   os.close(fd)
 finally:
  old_file.close()
 copystat(old_file_name, new_file_name)
 try:
  os.remove(old_file_name)
 except OSError as e:
  if ((getattr(e, 'winerror', 0) != 32) and (getattr(e, 'errno', 0) != 13)):
   raise

def file_move_safe ( old_file_name , new_file_name , chunk_size = ( 1024 * 64 ) , allow_overwrite = False ) : if _samefile ( old_file_name , new_file_name ) : return try : os . rename ( old_file_name , new_file_name ) return except OSError : pass old_file = open ( old_file_name , 'rb' ) try : fd = os . open ( new_file_name , ( ( ( os . O_WRONLY | os . O_CREAT ) | getattr ( os , 'O_BINARY' , 0 ) ) | ( ( ( not allow_overwrite ) and os . O_EXCL ) or 0 ) ) ) try : locks . lock ( fd , locks . LOCK_EX ) current_chunk = None while ( current_chunk != '' ) : current_chunk = old_file . read ( chunk_size ) os . write ( fd , current_chunk ) finally : locks . unlock ( fd ) os . close ( fd ) finally : old_file . close ( ) copystat ( old_file_name , new_file_name ) try : os . remove ( old_file_name ) except OSError as e : if ( ( getattr ( e , 'winerror' , 0 ) != 32 ) and ( getattr ( e , 'errno' , 0 ) != 13 ) ) : raise
moves a file from one location to another in the safest way possible .