python利用os模块进行增量备份

程序要求:
(1) 原工作目录为d:\working directory,此文件是磁盘已有的,请先行建立该文件夹以及内部的目录结构(d:\working directory\test(目录), d:\working directory\text.txt(文件)、d:\working directory\temp.txt(文件)
(2) 利用os的相关方法新建备份目录d:\backup
(3) 将d:\working directory中所有内容拷贝到d:\backup进行备份
(4) 当在d:\working directory中进行下面改变时:(a)新建d:\working directory\test\textbackup.txt (2)修改text.txt内容,将其内容变为"ok"。
(5) 要求本程序能自动对比工作目录和备份目录,仅更新所有改变的内容。

import os
import shutil
import schedule

workDir = r'D:\working directory'
backDir = r'D:\backup'
file = r'd:\working directory\test\textbackup.txt'

def work(): 
    if not os.path.isdir(backDir):
        os.mkdir(backDir)
        
    for c,d,f in os.walk(workDir):
        if f :
            for i in f:
                sourceFile = os.path.join(c + '\\' + i)
                aimDir = os.path.join(c.replace(workDir, backDir) +'\\' + i)

                if not os.path.isdir(c.replace(workDir, backDir) ):
                    os.mkdir(c.replace(workDir, backDir))                
                if not os.path.exists(aimDir):
                    shutil.copyfile(sourceFile, aimDir)
                    
    if os.path.exists(file):
        with open(workDir + "\\text.txt",'w' ) as f:
            f.write('ok')
            
schedule.every(1).second.do(work)
while True:
    schedule.run_pending()
#1秒钟备份一次

不难的,你对着你的流程进行实现即可。
注意:你要对比目录的话,就要考虑修改时间;