Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.032 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'subject:text': 0.05; 'subject:file': 0.07; 'collier': 0.09; 'restored': 0.09; 'subject:skip:a 10': 0.09; 'cc:addr:python-list': 0.11; 'wrote': 0.14; '"with"': 0.16; 'block.': 0.16; 'subject:Sort': 0.16; 'usage,': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'file.': 0.24; 'cc:2**0': 0.24; 'post': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'file:': 0.31; 'file': 0.32; 'open': 0.33; 'not.': 0.33; 'advice': 0.35; 'johnson': 0.35; 'received:google.com': 0.35; 'executing': 0.36; 'object,': 0.36; 'thanks': 0.36; 'changing': 0.37; 'does': 0.39; 'called': 0.40; 'how': 0.40; 'august': 0.61; 'skip:o 30': 0.61; 'tips': 0.61; 'simple': 0.61; 'guarantee': 0.63; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'close': 0.67; 'useful.': 0.68; 'obvious': 0.74; 'completion': 0.78; 'involved.': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; bh=Vm9wF3n5uH/Mc31bsV1TAWgBaBvF9TtRTmSvThj8rKU=; b=N0JKX1G6o1EuEd2ERAvY79R3tZBIAvNvmrIMwoogCE8OOshos8UPwpKDHSyRN8NSrA L4x7Bu4IVnzgLsgxZO/NkdcZh/RPbHD/3WHeMwUbw+JBlBziaYAEoyZQuBLLJ6dn2CTr DgjcDK+GuLiY+NAMywhZPfXIyVQ0X9uLSn+Dyx4IuZh66plaQQddwG9pc235jUuXQt7k T/dQ9gpfC77TDw+KVcSdLWg+s65Os3GVzsivtDUyQpLCvlewFZ5Tgc0rg61ndT/i/Zd9 u6dIj+EqW28CGs6eA0BGWmDbtV32q8j2AYianJ/mY9tzSEXsWFxrNzWKyeQJIxuBL5hz ExBg== X-Received: by 10.152.42.193 with SMTP id q1mr445785lal.65.1375789514877; Tue, 06 Aug 2013 04:45:14 -0700 (PDT) MIME-Version: 1.0 Sender: joshua.landau.ws@gmail.com In-Reply-To: References: <520058D7.10105@Gmail.com> <5200D210.9030003@Gmail.com> From: Joshua Landau Date: Tue, 6 Aug 2013 12:44:34 +0100 X-Google-Sender-Auth: 6OnSwl6yRV5-7QUyc1T_wE49Grg Subject: Re: Sort lines in a plain text file alphanumerically To: Chris Angelico Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375789517 news.xs4all.nl 16007 [2001:888:2000:d::a6]:60763 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52033 On 6 August 2013 11:52, Chris Angelico wrote: > On Tue, Aug 6, 2013 at 11:38 AM, Devyn Collier Johnson > wrote: >> with open('/home/collier/pytest/sort.TXT') as file: >> sorted(file, key=3Dstr.casefold, reverse=3DTrue) >> >> >> Thanks for the advice Joshua. I find these tips very useful. However, ho= w >> 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 invol= ved. > 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. To make it more obvious for Devyn=C2=B9, with is used any time you need a simple guarantee of when things are "closed", for any particular meaning. One popular usage, just as a taster, is with changing directories so that the original directory is restored upon completion of the sub-tasks. =C2=B9 And so it doesn't look like I wrote a whole post to be pedantic