python查找指定日期范围的文件并保持目录复制

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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("%Y-%m-%d")
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("%Y-%m-%d")
if file_time > time_modify and file_time 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运行该脚本。