Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52037 > unrolled thread
| Started by | Devyn Collier Johnson <devyncjohnson@gmail.com> |
|---|---|
| First post | 2013-08-06 08:29 -0400 |
| Last post | 2013-08-06 08:29 -0400 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Sort lines in a plain text file alphanumerically Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-08-06 08:29 -0400
| From | Devyn Collier Johnson <devyncjohnson@gmail.com> |
|---|---|
| Date | 2013-08-06 08:29 -0400 |
| Subject | Re: Sort lines in a plain text file alphanumerically |
| Message-ID | <mailman.249.1375792198.1251.python-list@python.org> |
On 08/06/2013 06:52 AM, Chris Angelico wrote:
> On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson
> <devyncjohnson@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(), then calls
> __enter__, and it guarantees to call __exit__ before executing any
> code following the with block. With a file object, __exit__ will close
> the file.
>
> ChrisA
Thanks! Now I see why using "with" is a better way to write the code.
DCJ
Back to top | Article view | comp.lang.python
csiph-web