2 ms·
Python: import sys data = open(sys.argv[1]).readlines() data.sort() open(sys.argv[2],'w').write(''.join(data))
by MereInterest 13y ago
Python:
import sys
data = open(sys.argv[1]).readlines()
data.sort()
open(sys.argv[2],'w').write(''.join(data))
- deleted 13y ago[deleted]
- benhoyt 13y agoNot bad, however with the ''.join(data) you're effectively doubling the amount of memory needed for large files, because it will build the entire output string in memory (and you've already got the list in memory). Better to use writelines(). You can also use sorted() to iterate over and sort the input lines automatically: sorted_lines = sorted(open(sys.argv[1])) open(sys.argv[2]).writelines(sorted_lines)