vedang vedang - 1 year ago 61
Python Question

Can we do a smart-copy in Python?

I'm a python newbie, and I'm writing a script to copy compiled files from one location to another. What I have is quite simple at the moment, something like this:

import os
import shutil

shutil.copy2 (src, dst)
#... many more shutil.copy commands
#src is a filename string
#dst is the directory where the file is to be copied

My problem is that many of the files being copied are large files, and not all of them are re-compiled in every compile cycle. Ideally, I would like to copy only the changed files in this script. Is there any way I can do this?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download