Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89591
| References | <8b2bd328-08a6-4211-85c4-8d117d1aae1e@googlegroups.com> |
|---|---|
| Date | 2015-04-30 10:33 +1000 |
| Subject | Re: seek operation in python |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.107.1430354001.3680.python-list@python.org> (permalink) |
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