-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_files.py
59 lines (52 loc) · 1.69 KB
/
merge_files.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
MAX_SIZE=50*1000*1000 #50mb
NUM_FILES=10
def merge_files(map,path2):
'''path2 is the location of raw files.'''
if not(os.path.isdir(path2)):
raise ValueError('location for processed files must be a directory.')
num=0
index=0
data=b''
for file in map[1]:
file_stats = os.stat(file)
size = file_stats.st_size
map[2].append([file,index,index+size])
index+=size
if len(data)+size<=MAX_SIZE:
with open(file,'rb') as tp:
data+=tp.read()
else:
temp=len(data)
with open(file,'rb') as tp:
data+=tp.read(MAX_SIZE-temp)
data_file_name=file_name(num,path2)
with open(data_file_name,'wb') as cf:
cf.write(data)
data=b''
num+=1
already_read=MAX_SIZE-temp
while size-already_read>MAX_SIZE:
data=tp.read(MAX_SIZE)
data_file_name = file_name(num, path2)
with open(data_file_name, 'wb') as cf:
cf.write(data)
already_read+=MAX_SIZE
data = b''
num += 1
data=tp.read()
data_file_name = file_name(num, path2)
with open(data_file_name, 'wb') as cf:
cf.write(data)
data = b''
num += 1
def file_name(num,path2):
q,r=divmod(num,NUM_FILES)
dir_path=os.path.join(path2,str(q))
if os.path.isdir(dir_path):
qwerty=3+4
else:
os.makedirs(dir_path)
result=os.path.join(dir_path,str(num))
result+='.baz'
return result