Sort lines in a plain text file alphanumerically

Chris Angelico rosuav at gmail.com
Tue Aug 6 09:15:06 EDT 2013


On Tue, Aug 6, 2013 at 12:44 PM, Joshua Landau <joshua at landau.ws> wrote:
> On 6 August 2013 11:52, Chris Angelico <rosuav at gmail.com> wrote:
>> On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson
>> <devyncjohnson at gmail.com> wrote:
>>>     with open('/home/collier/pytest/sort.TXT') as file:
>>>         sorted(file, key=str.casefold, reverse=True)
>>>
>>>
>>> Thanks for the advice Joshua. I find these tips very useful. However, how
>>> would I close the files, or would they close after the "with" construct is
>>> complete?
>>
>>
>> That's the whole point of 'with'. It calls open(),
>
> To be pedantic, it does not. open is called by the time the with gets involved.
>

The entire statement does. Yes, it's not 'with' that does that, but it
is called. Anyway, that's detaily stuff :)

ChrisA



More information about the Python-list mailing list