1.复制当日文件到指定目录(包含子目录),类似当日备份数据,复制到指定文件夹
#1.获取当前日期
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
#auther:pc
import os
import time
import shutil
now_time = time.localtime()
print(now_time)
#获取文件夹信息,和确认是否存在文件夹,如没有则创建
def get_filename(filepath,copy_path):
for root,dirs,files in os.walk(filepath):
# print(root)
for dir in dirs:
src_path = os.path.join(root,dir)
# print(src_path)
if os.path.isdir(src_path):
dir_ctime = time.localtime(os.path.getctime(src_path))
# print(dir_ctime)
des_path = os.path.join(copy_path,dir)
if not os.path.exists(des_path):
os.makedirs(des_path)
print("创建完成")
else:
print("文件路径存在")
#获取文件修改的最后时间
def get_filectime(path,cpath):
for f in os.listdir(path):
src_file_path = os.path.join(path,f)
des_file_path = os.path.join(cpath,f)
print(src_file_path)
print(des_file_path)
if os.path.isfile(src_file_path):
file_ctime = time.localtime(os.path.getctime(src_file_path))
if file_ctime.tm_year == now_time.tm_year and file_ctime.tm_mon == now_time.tm_mon and file_ctime.tm_mday == now_time.tm_mday:
shutil.copy(src_file_path,des_file_path)
elif os.path.isdir(src_file_path):
get_filectime(src_file_path,des_file_path)
else:
print("已完成")
break
#给出相应的路径,并调用函数
path = "D:\\bakfiles"
cp_path = "D:\\ftpbak"
get_filename(path,cp_path)
get_filectime(path,cp_path)