Python os 遍历文件夹

使用os库遍历文件夹,搜索文件夹中的文件,并把以文件的修改时间作为判断文件是否复制搬移的依据。

os.walk(filepath)可以获取文件夹下面的

  • top -- 是你所要遍历的目录的地址, 返回的是一个三元组(root,dirs,files)。

    • root 所指的是当前正在遍历的这个文件夹的本身的地址
    • dirs 是一个 list ,内容是该文件夹中所有的目录的名字(不包括子目录)
    • files 同样是 list , 内容是该文件夹中所有的文件(不包括子目录)
  • topdown --可选,为 True,则优先遍历 top 目录,否则优先遍历 top 的子目录(默认为开启)。如果 topdown 参数为 True,walk 会遍历top文件夹,与top 文件夹中每一个子目录。

  • onerror -- 可选,需要一个 callable 对象,当 walk 需要异常时,会调用。

  • followlinks -- 可选,如果为 True,则会遍历目录下的快捷方式(linux 下是软连接 symbolic link )实际所指的目录(默认关闭),如果为 False,则优先遍历 top 的子目录

现有需求如下,文件的重复的标准已文件的修改时间为准,对文件进行迁移操作,如上传文件夹下修改时间大于今天的文件。且把文件迁移动相应文件夹下并建立好预先命名的文件;

 

import os
import re
from dateutil.parser import parse
import shutil
from threading import Thread
dic  ={"AAA":"aaa", 
       "BBB":"bbb", 
       "CCC":"ccc", 
       "DDD":"ddd",
       "EEE":"eee", 
       "FFF":"fff", 
       "FfF":"fff",
       "GGG":"ggg",
       "Ggg":"ggg",
       "HHH":"hhh",
       }

base_dir = r'C:\测试'
target_dir = r'C:\搬移'
start_datetime = parse('2020/04/14') #开始时间
for root,dirs,files in os.walk(base_dir):
#     if datetime.datetime(*time.localtime(os.path.getmtime(os.path.join(root)))[0:7])>=start_datetime:
    for file in files:
#         print(root)
        if datetime.datetime(*time.localtime(os.path.getmtime(os.path.join(root,file)))[0:7])>=start_datetime:
            bu = re.findall('|'.join(dic.keys()),root.replace(base_dir,target_dir))
            makefilepath = root.replace(base_dir,target_dir).replace(bu[0],dic[bu[0]]) if bu !=[] else root.replace(base_dir,target_dir)
            if not os.path.exists(makefilepath):
                os.makedirs(makefilepath)

            shutil.copy(os.path.join(root,file),makefilepath)
            print(file)
#         print(dirs)

os.makedirs会递归创建文件夹。

dic:主要用于路径上的文件名的转换。

同时增设多线程检查相应文件夹下的日期。加快速度。

import os
import re
import datetime
import time
from dateutil.parser import parse
import shutil

def search_file_from_DMS(base_dir,target_dir,start_datetime):
    
    dic  ={"AAA":"aaa", 
       "BBB":"bbb", 
       "CCC":"ccc", 
       "DDD":"ddd",
       "EEE":"eee", 
       "FFF":"fff", 
       "FfF":"fff",
       "GGG":"ggg",
       "Ggg":"ggg",
       "HHH":"hhh",
       }

    base_dir1 = r'C:\测试'

    print("Thread start!")

    start_datetime = parse(start_datetime) #开始时间
    for root,dirs,files in os.walk(base_dir):

        for file in files:

            if datetime.datetime(*time.localtime(os.path.getmtime(os.path.join(root,file)))[0:7])>=start_datetime:
                bu = re.findall('|'.join(dic.keys()),root.replace(base_dir1,target_dir))
                makefilepath = root.replace(base_dir1,target_dir).replace(bu[0],dic[bu[0]]) if bu !=[] else root.replace(base_dir1,target_dir)
                if not os.path.exists(makefilepath):
                    os.makedirs(makefilepath)

                shutil.copy(os.path.join(root,file),makefilepath)

    print("Thread end!")





from threading import Thread

base_dir = r'D:\测试'
target_dir = r'C:\测试'
start_datetime = '2020/04/14'
files = os.listdir(base_dir)
n = 4
threads = []

for f in files:
    t = Thread(target=search_file_from_DMS,args=(os.path.join(base_dir,f),target_dir,start_datetime))
    threads.append(t)
    t.start()
for i in threads:
    i.join()
print("all thread end!")

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值