开发者

How to split folder into multiple subfolders which has html files

Trying to split a large folder which consists of 1 million html files. Want to split into subfolder with 10k each folder.

Want to split folder using python 3.9. OS: Window11

I tried below code

Split Large Folder into sub folder with limit

However, above method is开发者_如何学编程 splitting based on size. Please help me out


One solution would be to list all files in the folder, then proceed to put them in subfolders by batches.

import os

path: str = "./folder"


def displace_file(path1, path2):
    os.rename(path1, path2)


files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
batch_size = 10_000 # Your batch size, may be anything you want
min_index, max_index = 0, batch_size
count = 0
while max_index < len(files):
    os.mkdir(os.path.join(path, f"subdir{count}"))
    for file in files[min_index:max_index]:
        displace_file(f"{path}/{file}", f"{path}/subdir{count}/{file}")
    min_index += batch_size
    max_index += batch_size
    count += 1
# This part serves to also move the remainder of the files 
# (if you have 12k, this will move the remaining 2k)
os.mkdir(os.path.join(path, f"subdir{count}"))
for file in files[min_index:len(files)]:
    displace_file(f"{path}/{file}", f"{path}/subdir{count}/{file}")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜