Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'cpython': 0.05; '"""': 0.07; 'cest': 0.09; 'difference,': 0.09; 'rewrite': 0.09; 'def': 0.12; 'wrote': 0.14; '"with"': 0.16; '(about': 0.16; 'better:': 0.16; 'clear.': 0.16; 'closed.': 0.16; 'function?': 0.16; 'ironpython': 0.16; 'subject:python': 0.16; 'index': 0.16; 'wrote:': 0.18; 'module': 0.19; 'file,': 0.19; 'have:': 0.19; "skip:' 30": 0.19; 'thu,': 0.19; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'script': 0.25; 'switch': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'skip:g 30': 0.30; 'lines': 0.31; '>>>>': 0.31; 'file': 0.32; 'probably': 0.32; 'another': 0.32; 'open': 0.33; 'could': 0.34; 'problem.': 0.35; 'no,': 0.35; 'but': 0.35; 'version': 0.36; 'should': 0.36; 'seconds': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'solve': 0.60; 'skip:o 30': 0.61; 'first': 0.61; 'more': 0.64; '30,': 0.65; 'charset:windows-1252': 0.65; 'close': 0.67; 'received:74.208': 0.68; "'with'": 0.84; '2015': 0.84; 'promptly.': 0.84; 'received:74.208.4.194': 0.84 Date: Thu, 30 Apr 2015 04:31:26 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: seek operation in python References: <8b2bd328-08a6-4211-85c4-8d117d1aae1e@googlegroups.com> <87vbge9lm6.fsf@Equus.decebal.nl> <87y4lavy4q.fsf@Equus.decebal.nl> In-Reply-To: <87y4lavy4q.fsf@Equus.decebal.nl> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:m1lBATyS0pXOzT7OVabUDWryTTU0NXg7yVAWIyMi3xO6PF/Y+mu mL/PA3tKu0LUk2bvt2SFnHDBEztqmy5dyJiZni/AhE1qlASkhXN3L1ZY4RhWtaC5APpKw6R OwQEsfbz75VXoVmrdEGtibmeDhNx6GCCqONdRVsDjBpKVy7OyJbJtA/JMAObS9P5KeSByen Yg9J5pIJslrlIv5+GIXzQ== X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430382699 news.xs4all.nl 2908 [2001:888:2000:d::a6]:36295 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89613 On 04/30/2015 04:06 AM, Cecil Westerhof wrote: > Op Thursday 30 Apr 2015 09:33 CEST schreef Chris Angelico: > >> On Thu, Apr 30, 2015 at 4:27 PM, Cecil Westerhof wrote: >>>> with open("input.cpp") as f: >>>> lines = f.readlines() >>>> print(lines[7]) >>> >>> Is the following not better: >>> print(open('input.cpp', 'r').readlines()[7]) >>> >>> Time is the same (about 25 seconds for 100.000 calls), but I find >>> this more clear. >> >> The significant difference is that the 'with' block guarantees to >> close the file promptly. With CPython it probably won't make a lot >> of difference, and in a tiny script it won't do much either, but if >> you do this on Jython or IronPython or MicroPython or some other >> implementation, it may well make a gigantic difference - your loop >> might actually fail because the file's still open. > > I thought that in this case the file was also closed. But if that is > not the case I should think about this when I switch to another > version as CPython. > > I wrote a module where I have: > def get_indexed_message(message_filename, index): > """ > Get index message from a file, where 0 gets the first message > """ > > return open(expanduser(message_filename), 'r').readlines()[index].rstrip() > > But this can be used by others also and they could be using Jython or > another implementation. So should I rewrite this and other functions? > Or would it be OK because the open is in a function? > No, it's not going to close the file just because the open is in a function. The "with" construct was designed to help solve exactly this problem. Please use it. -- DaveA