I am trying to create a function that combines 2 text files, and sorts them, before writing the result to a new file. I have read the existing threads about sorting files, as well as the threads about merging files, but I haven't been able to find one that answers my question.
File1:
12:24:00: 14, 15, 16
20:13:09: 1, 2, 3
File2:
08:06:02: 43, 54, 10
15:16:05: 6, 2, 12
And the desired output would be this:
NewFile:
20:13:09: 1, 2, 3
15:16:05: 6, 2, 12
12:24:00: 14, 15, 16
08:06:02: 43, 54, 10
I originally tried to merge the content of both files into one list, and then sort it, before writing it to a new file, but that didn't seem to work. Here is what I have tried so far:
def mergeandsort(file1, file2, NewFile):
s1, s2, d=open(src1, 'r'), open(src2, 'r'), open(dst, 'w')
l=[]
l.append(list(s1))
l.append(list(s2))
n=sorted(l)
c=''.join(str(n))
d.write(c)
s1.close(); s2.close(); d.close()
I am new to Python, so any help would be appreciated!