Skip to content

Instantly share code, notes, and snippets.

@Lesmiscore
Last active October 22, 2021 11:31
Show Gist options
  • Save Lesmiscore/65e5b4808d4ac729fa7b55b4b6ac9184 to your computer and use it in GitHub Desktop.
Save Lesmiscore/65e5b4808d4ac729fa7b55b4b6ac9184 to your computer and use it in GitHub Desktop.
import itertools
from os import scandir
MAX_SCORE = 160 * 1024**3 # 160GiB
with scandir('.') as sd:
dirs = sorted((x for x in sd if x.is_dir()), key=lambda x: x.path)
def recursive(path):
with scandir(path) as adr:
for entry in adr:
if entry.is_file():
yield entry
elif entry.is_dir():
yield from recursive(entry)
score = MAX_SCORE
partitions = [[]]
part_sizes = [0]
for name in dirs:
size = sum(x.stat().st_size for x in recursive(name))
if size > score:
partitions.append([])
part_sizes.append(0)
score = MAX_SCORE
score -= size
partitions[-1].append(name)
part_sizes[-1] += size
for chunk_num, size, part in zip(itertools.count(1), part_sizes, partitions):
print('##########################################')
print(f'##### partition #{chunk_num}')
print('##########################################')
print('total size:', size)
print('\n'.join(x.path for x in part))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment