Skip to content Skip to sidebar Skip to footer

How To Copy Files Only If The Source Is Newer Than The Destination In Python?

I'm writing a script to copy compiled files from one location to another. What I have at the moment is something like this: import os import shutil shutil.copy2 (src, dst) #... m

Solution 1:

You could make use of the file modification time, if that's enough for you:

# If more than 1 second difference
if os.stat(src).st_mtime - os.stat(dest).st_mtime > 1:
    shutil.copy2 (src, dst)

Or call a synchronization tool like rsync.


Solution 2:

You can do a smart copy using distutils.file_util.copy_file by setting the optional argument: update=1.

There is also a version that copies entire directories with distutils.dir_util.copy_tree.

You can then verify that either of those is actually working and only copying required files by enabling logging:

import distutils.log
import distutils.dir_util

distutils.log.set_verbosity(distutils.log.DEBUG)
distutils.dir_util.copy_tree(
    src_dir,
    dst_dir,
    update=1,
    verbose=1,
)

which prints which files were copied.


Solution 3:


Solution 4:

If you don't have a definite reason for needing to code this yourself in python, I'd suggest using rsync. From it's man-page:

Rsync is a fast and extraordinarily versatile file copying tool. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination.

If you do wish to code this in Python, however, then the place to begin would be to study filecmp.cmp


Solution 5:

To build on AndiDog's answer, if you have files that might not exist in the destination folder:

# copy file if destination is older by more than a second, or does not exist
if (not os.path.exists(dest)) or (os.stat(src).st_mtime - os.stat(dest).st_mtime > 1) :
    shutil.copy2 (src, dest)

Post a Comment for "How To Copy Files Only If The Source Is Newer Than The Destination In Python?"