该脚本可以查找某个目录里指定时间范围的文件,并保存目录结构复制到新的路径。
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
| 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 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运行。