File size: 905 Bytes
6afd575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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")