python 拷贝文件创建目录_python 的文件目录拷贝转移,自动递归目录建立目录

#!/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')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值