fd_list open for in flist cmp_list for in fd_list while if len fd_list

 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
fd_list = [open(i, "r") for i in flist]
cmp_list = ["" for i in fd_list]
while 1:
if len(fd_list) == 0:
break
i=0
while i < len(fd_list):
# If true - it means that we have written this string into result file
# Or it means that we have just started the process
if cmp_list[i] == "":
cmp_list[i] = fd_list[i].readline()
# if file is finished - delete
if cmp_list[i] == "":
fd_list.pop(i).close()
del cmp_list[i]
else:
i+=1
else:
i+=1
# if no files left - exit
if len(cmp_list) == 0:
break
# comparing lines and writting the minimum one
obj_list = [LeafUrlByHits(string=i.strip()) for i in cmp_list]
min_item = min(obj_list)
for i in range(len(obj_list)):
if id(obj_list[i]) == id(min_item):
cmp_list[i] = ""
elif obj_list[i] == min_item:
min_item.setHits(min_item.getHits() + obj_list[i].getHits())
cmp_list[i] = ""
output_f.write(str(min_item) + "\n")