Read all inputfiles into one list, sort the result and write out the lines again:
from itertools import chain
from glob import glob
lines = list(chain.from_iterable(open(f, 'r') for f in glob('./files/*.??')))
lines.sort()
with open('listTogether.txt', 'w') as out:
out.writelines(lines)
If your files are large however, you want to sort the files separately, write out the sorted results, then merge the sorted files into the new output file, line by line, using a merge generator function.
You appear to be working with Windows files, which use \r\n
(carriage return plus linefeed) line endings; you could use universal lineending support and open the files with 'rU'
mode to always give you \n
line endings:
lines = list(chain.from_iterable(open(f, 'rU') for f in glob('./files/*.??')))
lines.sort()
with open('listTogether.txt', 'w') as out:
out.writelines(lines)
For more details on the U
mode character, see the open()
function call.
To remove any duplicates, you'd create a set instead of a list, then use sorted()
to write out a sorted sequence again:
lines = set(chain.from_iterable(open(f, 'rU') for f in glob('./files/*.??')))
with open('listTogether.txt', 'w') as out:
out.writelines(sorted(lines))