复制当日文件到指定目录(包含子目录)

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值