该脚本可以查找某个目录里指定时间范围的文件,并保存目录结构复制到新的路径。
import os
import datetime
import shutil
time_modify = str(datetime.date.today() - datetime.timedelta(days=1))
time_end = str(datetime.date.today())
path_src_web = "/opt/Seeyon/A8/base/upload"
path_dst_web = "/opt/dbbackup/seeyon_upload/"
print (time_modify,time_end)
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 < 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运行。