Spaces:
Sleeping
Sleeping
TintinMeimei
commited on
Commit
•
9eb8b77
1
Parent(s):
de5af80
split and merge
Browse files- tmp/split_and_merge.py +49 -0
tmp/split_and_merge.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
|
3 |
+
|
4 |
+
def split(fn, max_size):
|
5 |
+
f = open(fn, 'rb')
|
6 |
+
block_size = 1024
|
7 |
+
max_n = int(max_size/block_size)
|
8 |
+
i = 0
|
9 |
+
while True:
|
10 |
+
block = f.read(block_size)
|
11 |
+
if not block:
|
12 |
+
try:
|
13 |
+
w.close()
|
14 |
+
print(f"[Done] Completed.")
|
15 |
+
except:
|
16 |
+
pass
|
17 |
+
break
|
18 |
+
i += 1
|
19 |
+
if i % max_n == 1:
|
20 |
+
w = open(f"{fn}.part.{(i//max_n)+1}", 'wb')
|
21 |
+
print(f"[Start] Creating {fn}.part.{(i//max_n)+1} ...")
|
22 |
+
w.write(block)
|
23 |
+
|
24 |
+
if i % max_n == 0:
|
25 |
+
w.close()
|
26 |
+
print(f"[Done] Closing {fn}.part.{(i//max_n)} ...")
|
27 |
+
|
28 |
+
|
29 |
+
def merge(fn):
|
30 |
+
import glob
|
31 |
+
fns = sorted(glob.glob(f"{fn}*"))
|
32 |
+
w = open(f"{fn}.copy", 'wb')
|
33 |
+
for fn in fns:
|
34 |
+
f = open(fn, 'rb')
|
35 |
+
w.write(f.read())
|
36 |
+
f.close()
|
37 |
+
w.close()
|
38 |
+
|
39 |
+
|
40 |
+
if __name__ == '__main__':
|
41 |
+
cmd = sys.argv[1]
|
42 |
+
fn = sys.argv[2]
|
43 |
+
|
44 |
+
if cmd == 'split':
|
45 |
+
max_size = int(sys.argv[3])
|
46 |
+
split(fn, max_size)
|
47 |
+
elif cmd == 'merge':
|
48 |
+
merge(fn)
|
49 |
+
|