1 回答
TA贡献1836条经验 获得超5个赞
因为split不支持按文件数拆分,所以我实现了一个 Python 实用程序:
import tarfile
import glob
import os
import shutil
def make_a_split(input_dir, split_num):
print("split {}".format(split_num))
tar_output = tarfile.open("split_" + str(split_num) + ".tar.gz", "w:gz")
for file_name in glob.glob(os.path.join(input_dir, "*")):
#print(" Adding %s..." % file_name)
tar_output.add(file_name, os.path.basename(file_name))
tar_output.close()
shutil.rmtree(tmp_output_dir)
print("split {} done".format(split_num))
count_per_split = 300000
split = 1
tmp_output_dir = "tmp/"
tar = tarfile.open('your.tar.gz')
for idx, tarinfo in enumerate(tar):
tar.extract(tarinfo, tmp_output_dir)
if idx > 0 and idx % count_per_split == 0:
make_a_split(tmp_output_dir, split)
split += 1
tar.close()
# did we not remove the temp dir? It means we have seen less than count_per_split and need
# to make a split
if os.path.exists(tmp_output_dir):
make_a_split(tmp_output_dir, split)
添加回答
举报