coolai / tmp /split_and_merge.py
TintinMeimei's picture
split and merge
9eb8b77
raw
history blame
1.08 kB
import sys
def split(fn, max_size):
f = open(fn, 'rb')
block_size = 1024
max_n = int(max_size/block_size)
i = 0
while True:
block = f.read(block_size)
if not block:
try:
w.close()
print(f"[Done] Completed.")
except:
pass
break
i += 1
if i % max_n == 1:
w = open(f"{fn}.part.{(i//max_n)+1}", 'wb')
print(f"[Start] Creating {fn}.part.{(i//max_n)+1} ...")
w.write(block)
if i % max_n == 0:
w.close()
print(f"[Done] Closing {fn}.part.{(i//max_n)} ...")
def merge(fn):
import glob
fns = sorted(glob.glob(f"{fn}*"))
w = open(f"{fn}.copy", 'wb')
for fn in fns:
f = open(fn, 'rb')
w.write(f.read())
f.close()
w.close()
if __name__ == '__main__':
cmd = sys.argv[1]
fn = sys.argv[2]
if cmd == 'split':
max_size = int(sys.argv[3])
split(fn, max_size)
elif cmd == 'merge':
merge(fn)