Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89591
| 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 | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.027 |
| X-Spam-Evidence | '*H*': 0.95; '*S*': 0.00; 'say,': 0.05; 'memory.': 0.07; 'if,': 0.09; 'latter': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'changes': 0.15; '(counting': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'parentheses': 0.16; 'statement.': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'thu,': 0.19; 'code,': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'byte': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'changes,': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'lines': 0.31; 'prints': 0.31; 'file': 0.32; 'probably': 0.32; 'compatible': 0.32; 'supposed': 0.32; 'text': 0.33; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'example,': 0.37; 'rather': 0.38; 'even': 0.60; 'read': 0.60; 'is.': 0.60; 'back': 0.62; 'name': 0.63; '30,': 0.65; "'with'": 0.84; '2015': 0.84; 'promptly.': 0.84; 'safer': 0.84; 'seventh': 0.84; 'mistake': 0.91; 'to:none': 0.92; 'deal,': 0.93 |
| 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=hhImMpS9y6nrKAna6KRyQkwZrtePx4I8lscVcmXpe6I=; b=Xm7HtlX6dW1gl7NGKLXonmZXvV4WqpRv4ugsE53i6MGt/6qLwjqr0c4bf0nH/J1/h2 IUqbAdIReJjpJ+4j3ji27gVJpO4Jk7OELhdxIagPU6BjHw4mO1NNezqa9Awm9RUI+hf1 zG4x1SPFDYIweMWTxl22XLpI7CD4UD9lfP3bGzQET4G+bo9eGQ+o+ko9bp0xdqEBYT78 T2kjIHwv3a5HCsawi2x1rBN4qVXvWgduTPlDkQhsHVitLyCt/VXr4iO1pT9BEZhBDrWu v6crv40xDJ0g+gl03SJtyFwzZuwhkcrxTY+RqedYlsg5whBRYf/TfFk+naWVKjtNKR8e xz0g== |
| MIME-Version | 1.0 |
| X-Received | by 10.50.108.115 with SMTP id hj19mr311704igb.34.1430353999285; Wed, 29 Apr 2015 17:33:19 -0700 (PDT) |
| In-Reply-To | <8b2bd328-08a6-4211-85c4-8d117d1aae1e@googlegroups.com> |
| References | <8b2bd328-08a6-4211-85c4-8d117d1aae1e@googlegroups.com> |
| Date | Thu, 30 Apr 2015 10:33:19 +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.107.1430354001.3680.python-list@python.org> (permalink) |
| Lines | 34 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1430354001 news.xs4all.nl 2969 [2001:888:2000:d::a6]:60692 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:89591 |
Show key headers only | View raw
On Thu, Apr 30, 2015 at 4:08 AM, siva sankari R <buddingrose11@gmail.com> wrote:
> file=open("input","r")
> line=file.seek(7)
> print line
>
> The above code is supposed to print a line but it prints "none". I don't know where the mistake is. Help.!
Going right back to the beginning... Are you aware that 'seek' works
with byte positions? On a text file, you can't even do this, and even
on a byte file, it won't give you the seventh line.
If, as you say, it's only some eighty lines of code, the best solution
is probably the simplest: read the whole file into memory.
with open("input.cpp") as f:
lines = f.readlines()
print(lines[7])
That will print out the seventh line (counting from zero; take
lines[6] if you want to count from one), rather than seeking to byte
position 7 and printing out from there to the end of a line.
I've made a few other changes in the example, too:
1) Not that it's a big deal, but I used "f' rather than "file",
because the latter is a built-in name in Python 2, and it's safer not
to shadow.
2) A 'with' block ensures that the file is closed promptly.
3) Parentheses around your 'print' make it compatible with the
function form as well as the statement.
They're all small changes, but clean code is good code :)
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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