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

后端 未结 7 494
囚心锁ツ
囚心锁ツ 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:13

    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)
    

提交回复
热议问题