Sort lines in a plain text file alphanumerically

alex23 wuwei23 at gmail.com
Tue Aug 6 00:33:17 EDT 2013


On 6/08/2013 1:49 PM, alex23 wrote:
> On 6/08/2013 1:12 PM, Joshua Landau wrote:
>> Because it's bad to open files without a with unless you know what
>> you're doing, use a with:
>>
>>      with open('/home/collier/pytest/__sort.TXT') as file:
>>          sorted(file, key=str.casefold, reverse=True)
>
> Shouldn't that be:
>
>      with open('/home/collier/pytest/__sort.TXT') as file:
>          data = file.readlines()
>          sorted(data, key=str.casefold, reverse=True)

Hmm, I take that back entirely. Your version does work. Weirdly, I tried 
yours under both 2.7 & 3.2 without it working at all, but a subsequent 
attempt did. Sorry for the noise.



More information about the Python-list mailing list