Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!news.albasani.net!weretis.net!feeder4.news.weretis.net!storethat.news.telefonica.de!telefonica.de!news.panservice.it!feed.xsnews.nl!border02.ams.xsnews.nl!feeder04.ams.xsnews.nl!abp002.ams.xsnews.nl!frontend-F09-04.ams.news.kpn.nl From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: seek operation in python Organization: Decebal Computing References: <8b2bd328-08a6-4211-85c4-8d117d1aae1e@googlegroups.com> X-Face: "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR,v+Pti8=Vi/Z"g^?b"E X-Homepage: http://www.decebal.nl/ Date: Thu, 30 Apr 2015 08:27:29 +0200 Message-ID: <87vbge9lm6.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:Z8ujf1hljM9fAQ8nefjG+mfwX+U= MIME-Version: 1.0 Content-Type: text/plain Lines: 31 NNTP-Posting-Host: 81.207.62.244 X-Trace: 1430375329 news.kpn.nl 19259 81.207.62.244@kpn/81.207.62.244:47961 Xref: csiph.com comp.lang.python:89606 Op Thursday 30 Apr 2015 02:33 CEST schreef Chris Angelico: > On Thu, Apr 30, 2015 at 4:08 AM, siva sankari R 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]) 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. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof