该脚本查找../upload20210929/2007目录下时间戳大于2021-10-18小于2021-10-21的文件并保持目录结构复制到./backup2/中。

import os
import sys
import datetime, time
import shutil
app_name = "2007"
project_home = "../upload20210929/"
update_home = "./backup2/"
time_modify = "2021-10-18"
time_end = "2021-10-21"
#time_modify = datetime.datetime.now().strftime(dateFormat)
path_src_web = os.path.join(project_home, app_name)
path_dst_web = os.path.join(update_home, app_name)
all_files = []
all_new_files = []
def get_all_file(rawdir):
    all_file_list = os.listdir(rawdir)
    for f in all_file_list:
        filepath = os.path.join(rawdir, f)
        if os.path.isdir(filepath):
            get_all_file(filepath)
        if not os.path.isdir(filepath):
            all_files.append(filepath)
    return all_files
def get_new_file():
    for f in get_all_file(path_src_web):
        file_time = datetime.datetime.fromtimestamp(os.path.getmtime(f)).strftime(dateFormat)
        if file_time > time_modify and file_time < time_end:
            #cmd_xcopy = '%s %s' % (f, file_time)
            all_new_files.append(f)
            #print cmd_xcopy
    return all_new_files
def copy_web_files():
    print (path_src_web)
    print (path_dst_web)
    new_files = get_new_file()
    for f in new_files:
        target_file = f.replace(path_src_web, path_dst_web)
        #print target_file
        head, tail = os.path.split(target_file)
        #print(head)
        if not os.path.exists(head):
            os.makedirs(head)
        shutil.copyfile(f, target_file)
        #shutil.copy(f, path_dst_web)
        print (f)
    file_size = len(new_files)
    if(file_size > 0):
        print("copy_web_files success! count: %s  target:%s." %(file_size,path_dst_web))
    else:
        print("copy_web_files error , no files for copy...")
if __name__ == '__main__':
    copy_web_files()

推荐python3运行该脚本。