import os import tarfile from math import ceil, floor directory = "results_10M_val" all_files = [] print(f"enumerating files {directory}") for root, dirs, files in os.walk(directory): for file in files: file_path = os.path.join(root, file) all_files.append(file_path) all_files = sorted(all_files) samples_per_tar = 1000 total = ceil(len(all_files) / samples_per_tar) print(len(all_files)) output_tar = f"{directory}_{0}-of-{total}.tar" tar = tarfile.open(output_tar, "w") # for _idx in range(total): for idx, f in enumerate(all_files): print(output_tar, f"[{idx % samples_per_tar}/{samples_per_tar}]", f) tar.add(f) if (idx + 1) % samples_per_tar == 0: tar.close() output_tar = f"{directory}_{(idx + 1) // samples_per_tar}-of-{total}.tar" tar = tarfile.open(output_tar, "w") print("switch files") # break print("finish all")