shutil.copy2()
is perfectly capable of copying the metadata containing the date and time of the last file modification:
shutil.copy2( src, dst, * , follow_symlinks=True )
Identical to copy()
except that copy2()
also Attempts to preserve all
file Metadata.
copy2()
you use copystat()
to copy the file Metadata. Please see
copystat()
for more information about Platform support for modifying
Symbolic link Metadata.
Follows a code able to ensure that the source will be copied to the destination along with all possible metadata. If the destination has a creation date older than the origin, the destination will be overwritten with the origin:
import shutil
import os
def sincronizar( src, dst ):
# Verifica se origem existe
if not os.path.exists( src ):
return -1; # Erro
# Copia se o arquivo de destino
# nao existir
if not os.path.exists( dst ):
shutil.copy2( src, dst )
return 1; # Sucesso
# Recupera metadados da origem e do destino
dst_info = os.stat(dst)
src_info = os.stat(src)
# Copia somente se a data de modificao do arquivo
# de destino estiver mais antiga que 1 segundo
if( src_info.st_mtime - dst_info.st_mtime > 1 ):
shutil.copy2( src, dst )
return 1 # Sucesso
return 0 # Sucesso (sem copia)