-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile_utils.py
More file actions
38 lines (29 loc) · 1.23 KB
/
file_utils.py
File metadata and controls
38 lines (29 loc) · 1.23 KB
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
import os
import glob
def get_chunk_number_from_chunk_filename(filename):
splitNames = filename.split(".")
totalSplits = len(splitNames)
return int(splitNames[totalSplits - 2])
def get_all_chunk_number_available(directory, filename):
path = os.path.join(directory, filename+".*"+".chunk")
list_of_chunks = glob.glob(path)
return [get_chunk_number_from_chunk_filename(chunk) for chunk in list_of_chunks]
def has_file(directory, filename):
return os.path.isfile(os.path.join(directory, filename))
def remove_all_associated_chunks(directory, filename):
path = os.path.join(directory, filename+".*"+".chunk")
for filename in glob.glob(path):
os.remove(filename)
return
def combine_chunks(directory, filename, num_of_keys, chunk_size):
chunk_index = 0
new_file_directory = os.path.join(directory, filename)
while(chunk_index < num_of_keys):
chunk_file_name = filename + "." + str(chunk_index) + ".chunk"
chunk_file_directory = os.path.join(directory, chunk_file_name)
with open(new_file_directory, 'ab') as new_file:
with open(chunk_file_directory, 'rb') as chunk_file:
new_file.write(chunk_file.read(chunk_size))
chunk_index+=1
if(chunk_index == num_of_keys):
remove_all_associated_chunks(directory, filename)