#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by weilai on 2016/11/21
__author__ = 'weilai'
"""
@version: v0.5
@author: weilai
@license: Apache Licence
@contact:
@site: http://showmymind.com/
@software: PyCharm Community Edition
@file: wl_copy.py
@time: 2016/11/21 21:50
"""
import os
import shutil
import datetime
import time
class file_entity:
file_path = ''
file_name = ''
def __init__(self, path, file):
self.file_path = path
self.file_name = file
class copy_work:
def __init__(self):
self.source_path = ''
self.target_path = ''
self.source_file_list = []
self.target_file_list = []
self.interval = '\\'
self.filter = [self.suffix_filter, self.time_filter]
# 定义 拷贝文件后缀和时间范围
# 文件后缀,以下文件后缀会被拷贝到目标目录
self.file_suffix = [".dll", ".pdb", ".xml"]
# 时间范围 以下数字分钟以内的修改文件才会被转移
self.time_range = 30
# 发布后提示语,可直接拷贝到QQ目录通知相关人员提取
self.report_str = "\n 已经发布 请提! 谢谢!!(v0.5)"
self.version = '0.5'
self.total_count = 0
self.copy_all = False
def load_source(self, curr_path, deep=1):
self.total_count = 0;
file_list = os.listdir(curr_path)
for num in range(len(file_list)):
filename = file_list[num]
if os.path.isdir(curr_path + self.interval + filename):
self.load_source(curr_path + self.interval + filename, deep + 1)
else:
curr = file_entity(curr_path, filename)
curr_f_name = curr.file_path + self.interval + curr.file_name
if(self.filter[0](curr_f_name) and self.filter[1](curr_f_name)):
self.source_file_list.append(curr)
self.total_count += 1
print(curr_f_name)
def make_target_list(self):
for source_file in self.source_file_list:
tar_path = source_file.file_path.replace(self.source_path, self.target_path)
curr = file_entity(tar_path, source_file.file_name)
self.target_file_list.append(curr)
def move_file(self, sourceDir, targetDir):
"""拷贝文件,从源目录到目标目录"""
shutil.copy(sourceDir, targetDir)
def mkdir(self, path):
"""创建目录"""
# 引入模块
path = path.strip()
path = path.rstrip("\\")
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
def suffix_filter(self, full_path):
if self.copy_all:
return True
is_filtrate = False
for s in self.file_suffix:
last_pos = len(s)
last_suffix = full_path[-last_pos:]
# print(last_suffix + s)
if last_suffix == s:
is_filtrate = True
break
return is_filtrate
def time_filter(self, file_path):
is_filtrate = False
now = datetime.datetime.now()
just_now = now - datetime.timedelta(minutes=self.time_range)
curr_file_m_time = self.get_filem_time(file_path)
# print(file_path + "--" + curr_file_m_time.strftime('%Y-%m-%d %H:%M:%S'))
if curr_file_m_time > just_now:
is_filtrate = True
return is_filtrate
def get_filem_time(self, file_path):
temp_file_m_time = time.localtime(os.stat(file_path).st_mtime)
file_m_time = datetime.datetime(*tuple(temp_file_m_time)[:6])
return file_m_time
def copy_file(self, source, target):
self.source_path = source
self.target_path = target
self.mkdir(self.target_path)
self.load_source(source)
self.make_target_list()
for i in range(len(self.source_file_list)):
sou_f = self.source_file_list[i]
tar_f = self.target_file_list[i]
sou_full = sou_f.file_path + self.interval + sou_f.file_name
tar_full = tar_f.file_path + self.interval + tar_f.file_name
self.mkdir(tar_f.file_path)
self.move_file(sou_full, tar_full)
#self.report()
def report(self):
print(self.source_path + self.report_str)
# 用法
cw = copy_work()
# 设置 文件后缀过滤,此后缀才会被拷贝
cw.file_suffix = [".dll", ".pdb", ".xml"]
# 设置 时间,修改时间是30分钟以内的,才会被拷贝
cw.time_range = 30
# 执行拷贝
cw.copy_file('Z:\\V001\\web\\201611211920', 'E:\\x')