I have 540 files in my directory. All files' data formats are given below:
File input.txt
class confidence Xmin Ymin Xmax Ymax
7 0.3456900 89 8 39 53
6 0.0123457 2 1 23 43
File result.txt
class confidence Xmin Ymin Xmax Ymax
6 0.0123457 2 1 23 43
7 0.3456900 89 8 39 53
I've solved this problem for reading a single file. Code is given below:
This is my single text file compare code. It's working. But I have 540 text files, and I want to sort files like this. How can I sort multiple files in my directory by the same process ? I need to make a specific file name for each file.
from collections import defaultdict
maxima = defaultdict(int)
with open('F:\GGR\grnd.txt', 'r') as ifh:
for line in ifh:
key, value = line.rsplit(None, 1)
value = int(value)
if value > maxima[key]:
maxima[key] = value
with open('output.txt', 'w') as ofh:
for key in sorted(maxima):
ofh.write('{} {}\n'.format(key, maxima[key]))