#!/usr/bin/env python


from pathlib import Path
from random import shuffle
from shutil import copy


# configs

N = 10000
SAMPLED_DIR_PATH = Path('sampled/')

# mkdir if doesn't exist

if not SAMPLED_DIR_PATH.exists():
    SAMPLED_DIR_PATH.mkdir()

# sample and copy

paths = [p for p in Path('preprocessed/').iterdir()]
shuffle(paths)

for p in paths[:N]:
    copy(str(p), str(SAMPLED_DIR_PATH / p.name))

#!/usr/bin/env python


from pathlib import Path
from random import sample
from os import remove


# configs

N = 10000

# remove unsampled

paths = [path for path in Path('preprocessed/').iterdir()]
paths_len = len(paths)

if paths_len <= N:
    raise RuntimeError('file count {:,} <= N {:,}'.format(paths_len, N))

for path in sample(paths, paths_len-N):
    remove(str(path))

Use removing rather than copying
Use removing rather than copying
