批量打包文件数据

数据批量分包

import os
import shutil
import math

def databag(path,folderPath, number):

        path = path.strip("\"")
        file_list = os.listdir(path) #源文件名称列表
        Number = math.ceil(len(file_list) / number) #目标文件夹数量
        folderNumber = -1 #起始文件夹id ,-1是因为0 % 任意数 = 0
        sort_folder_number = [x for x in range(0,Number)]

        #  创建文件夹
        for foldernumber in sort_folder_number:
            new_folder_path = os.path.join(folderPath,'%s'%foldernumber)#new_folder_path is ‘folderPath\number'

            if not os.path.exists(new_folder_path):
                os.makedirs(new_folder_path)
                print("new a floder named "+str(foldernumber)+'at the path of '+ new_folder_path)

        #分包
        for i in range(0,len(file_list)):
            old_file = os.path.join(path, file_list[i])
            if os.path.isdir(old_file):
                '''if the path is a folder,program will pass it'''
                print('img does not exist ,path=' + old_file+' it is a dir' )
                pass
            elif not os.path.exists(old_file):
                '''if the path does not exist,program will pass it'''
                print('img does not exist ,path='+old_file)
                pass
            else:
                '''define the number,it decides how many imgs each people process'''
                if(0 == (i % number)): #导致folderNumber = -1 : 0 % 任意数 = 0
                    folderNumber += 1
                new_file_path = os.path.join(folderPath,'%s'%(folderNumber))
                if not os.path.exists(new_file_path):
                    break
                shutil.move(old_file,new_file_path)
                print(old_file+'is successfully moved to '+new_file_path)

class_names = os.listdir('D:\PycharmProjects\Classification1\datasets/train3')
for i in range(20):
        flodername = os.listdir('D:\PycharmProjects\Classification1\datasets/train3/'+class_names[i])
        path='D:\PycharmProjects\Classification1\datasets/train3/'+class_names[i]
        databag(path, path, 100)
        print(path)
        '''file_list = os.listdir(path)
        print(file_list)'''

        '''for p in range(100):
            des='D:\PycharmProjects\Classification1\datasets/train3/'+class_names[i]+'/0/'+file_list[p]
            print(des)'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值