将一个大文件夹拆分为多个小文件夹

任务:要将一个文件夹下的14694条音频文件转换为文本(python调用接口)
因为一次运行的次数较多时,服务不稳,会报错
在这里插入图片描述
想到将所有的音频文件拆分为小文件夹,每个文件夹里包含3000条音频,总共有5个文件夹。

import os
import shutil 

def mkdir(path): 
	folder = os.path.exists(path) 
	if not folder:                   #判断是否存在文件夹如果不存在则创建为文件夹
		os.makedirs(path)            #makedirs 创建文件时如果路径不存在会创建这个路径 
	else:
		pass

path = "G:/ASR/RecFile0228_shanghai"
dirs = os.listdir(path)
for index,file in enumerate(dirs):
    print(index,":",file)    
    for i in range(5) :
        if 3000*i <= index and index < 3000*(i + 1):
            if index % 3000 == 0:
                target_file = "G:\\ASR\\RecFile0228_3000\\RF_3000_%s"%int(index/3000)
                mkdir(target_file)
            else:
                pass
            source_file = os.path.join(path,file)
            print("source_file:",source_file)
            shutil.copy(source_file,target_file)
        else:
            pass

os.mkdirs() :创建文件夹
shutil.copy(source_file,target_file) :将source_file(文件)复制到 target_file(文件夹)

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值