参考
import os
import shutil
"""
一个文件夹划分成多个子文件夹,每个子文件夹 num个文件,新建文件夹放入
"""
file_path = 'D:\新建文件夹\\'
num = 100000
list_ = os.listdir(file_path)
if num > len(list_):
print('num长度需小于:', len(list_))
exit()
if int(len(list_) % num) == 0:
num_file = int(len(list_) / num)
else:
num_file = int(len(list_) / num) + 1
cnt = 0
for n in range(1, num_file + 1):
new_file = os.path.join(file_path + str(n))
if os.path.exists(new_file + str(cnt)):
print('该路径已存在,请解决冲突', new_file)
exit()
print('创建文件夹:', new_file)
os.mkdir(new_file)
list_n = list_[num * cnt:num * (cnt + 1)]
for m in list_n:
old_path = os.path.join(file_path, m)
new_path = os.path.join(new_file, m)
shutil.copy(old_path, new_path)
cnt += 1