用python把一个文件夹按照一定数目分割成多个小文件夹(可用于百度网盘单次上传文件数量限制)


import os
import shutil
import math


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

root_path = r"D:\Project\Excel\test"#选择需要分离文件夹的上一级目录
folder_max_pics = 500
works_folders = os.listdir(root_path)

for works_folder in works_folders:
    works_folder_path = root_path + "/" + works_folder
    pictures = os.listdir(works_folder_path)
    division = len(pictures) / folder_max_pics
    if division > 1:
        for j in range(1, math.ceil(division)):
            works_folder_path_add = works_folder_path + "--" + str(j)
            mkdir(works_folder_path_add)
    for k, picture in enumerate(pictures):
        a = k + 1
        if a > folder_max_pics:
            
            division_ceil = math.ceil(a / folder_max_pics)
            source_file = os.path.join(works_folder_path, picture)
            # print("source_file:", source_file)
            
            target_path = works_folder_path + "--" + str(division_ceil - 1)
            shutil.move(source_file, target_path)




你可能感兴趣的:(用python把一个文件夹按照一定数目分割成多个小文件夹(可用于百度网盘单次上传文件数量限制))