How to copy files only if the source is newer than the destination in Python?

后端 未结 7 484
囚心锁ツ
囚心锁ツ 2021-01-11 19:44

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         


        
7条回答
  •  清酒与你
    2021-01-11 20:00

    How would you like to look for changed files? You can just use os.path.getmtime(path) on the src and check whether that's newer than some stored timestamp (the last time you copied for instance) or use a filecmp.cmp(f1, f2[, shallow]) to check whether a file is newer.

    Watch out with filecmp.cmp, you also copy the stat (copy2) so you have to check wether a shallow compare is good enough for you.

提交回复
热议问题