Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89609

Re: seek operation in python

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 <rosuav@gmail.com>
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> <mailman.107.1430354001.3680.python-list@python.org> <87vbge9lm6.fsf@Equus.decebal.nl>
Date Thu, 30 Apr 2015 17:33:25 +1000
Subject Re: seek operation in python
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.118.1430379207.3680.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Thu, Apr 30, 2015 at 4:27 PM, Cecil Westerhof <Cecil@decebal.nl> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

seek operation in python siva sankari R <buddingrose11@gmail.com> - 2015-04-29 11:08 -0700
  Re: seek operation in python Joel Goldstick <joel.goldstick@gmail.com> - 2015-04-29 14:17 -0400
    Re: seek operation in python siva sankari R <buddingrose11@gmail.com> - 2015-04-29 11:26 -0700
      Re: seek operation in python Billy Earney <billy.earney@gmail.com> - 2015-04-29 13:42 -0500
      Re: seek operation in python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-29 21:53 +0100
        Re: seek operation in python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-30 10:19 +1000
  Re: seek operation in python MRAB <python@mrabarnett.plus.com> - 2015-04-29 19:50 +0100
  Re: seek operation in python John Gordon <gordon@panix.com> - 2015-04-29 18:53 +0000
  Re: seek operation in python Chris Angelico <rosuav@gmail.com> - 2015-04-30 10:33 +1000
    Re: seek operation in python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 08:27 +0200
      Re: seek operation in python Chris Angelico <rosuav@gmail.com> - 2015-04-30 17:33 +1000
        Re: seek operation in python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 10:06 +0200
          Re: seek operation in python Dave Angel <davea@davea.name> - 2015-04-30 04:31 -0400
            Re: seek operation in python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 11:06 +0200
              Re: seek operation in python Chris Angelico <rosuav@gmail.com> - 2015-04-30 19:39 +1000
          Re: seek operation in python Larry Hudson <orgnut@yahoo.com> - 2015-04-30 12:38 -0700
            Re: seek operation in python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 22:50 +0200
              Re: seek operation in python Larry Hudson <orgnut@yahoo.com> - 2015-04-30 19:42 -0700
  Re: seek operation in python Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 14:31 +0200

csiph-web