Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.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.032 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'cpython': 0.05; 'difference,': 0.09; 'cc:addr:python-list': 0.11; '(about': 0.16; 'better:': 0.16; 'clear.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ironpython': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'script': 0.25; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'file': 0.32; 'probably': 0.32; 'but': 0.35; 'received:google.com': 0.35; 'seconds': 0.37; 'pm,': 0.38; 'skip:p 20': 0.39; 'more': 0.64; '30,': 0.65; 'close': 0.67; "'with'": 0.84; '2015': 0.84; 'promptly.': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=ZINtO9rsBkvmave9EycAXn4QQseS9apy+xkvm6XZIZs=; b=D8pQC3L2qoOovnSCh/zRgEA27SFdknAEj9rxe+j6EPakBaNh4MpDJMDAsZf8JjtHbO FIU2Ks1p7YxKZZXZECBhfP7kgu3rPRd1c4BJgYj39dHtRNM4MzOMG+sYqdbDusExDxvc azfbkhVOPvuFTZSonaNGQLvoL/A5WLkj8qfcKletQ++b720qm5vqcV1hom4xSQ3dRzou tnhEKBCBJxcz49pUbimLfL3vhmkHK0Pl3EvwUc1RCL1eDmpwIvpxCwdHTuw6sCYds8+p bJ/gZQN+I9PKh2kMlwTihQbK/IISr6RB2UgW+PLkiz6xu7aO/bih3flI/iNIyoqzHHnC I5vA== MIME-Version: 1.0 X-Received: by 10.50.176.137 with SMTP id ci9mr1872119igc.2.1430379205421; Thu, 30 Apr 2015 00:33:25 -0700 (PDT) In-Reply-To: <87vbge9lm6.fsf@Equus.decebal.nl> References: <8b2bd328-08a6-4211-85c4-8d117d1aae1e@googlegroups.com> <87vbge9lm6.fsf@Equus.decebal.nl> Date: Thu, 30 Apr 2015 17:33:25 +1000 Subject: Re: seek operation in python From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430379207 news.xs4all.nl 2870 [2001:888:2000:d::a6]:41065 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89609 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. ChrisA