Similar to what Hugh recommended (but different in that it is not a pure Python solution), you can sort the file in chunks. For example, divide the file into 26 other files - A.txt, B.txt, C.txt, etc. Sort each of them separately, and then combine them to get the final result.
The main thing to keep in mind that the first pass through the source file is to simply split the lines into their first letters. Only after that you start sorting through each file. A simple cat A.txt B.txt ... will handle the rest.
source share