Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed.pionier.net.pl!feed.xsnews.nl!border03.ams.xsnews.nl!feeder04.ams.xsnews.nl!abp001.ams.xsnews.nl!frontend-F09-12.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> <87vbge9lm6.fsf@Equus.decebal.nl> <87y4lavy4q.fsf@Equus.decebal.nl> 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 11:06:10 +0200 Message-ID: <87twvyvvct.fsf@Equus.decebal.nl> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:QnHMfrtmb1IfsRu/tbTIxNn/+WE= MIME-Version: 1.0 Content-Type: text/plain Lines: 63 NNTP-Posting-Host: 81.207.62.244 X-Trace: 1430385291 news.kpn.nl 18624 81.207.62.244@kpn/81.207.62.244:36738 Xref: csiph.com comp.lang.python:89615 Op Thursday 30 Apr 2015 10:31 CEST schreef Dave Angel: > On 04/30/2015 04:06 AM, Cecil Westerhof wrote: >> Op Thursday 30 Apr 2015 09:33 CEST schreef Chris Angelico: >> >>> 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. >> >> I thought that in this case the file was also closed. But if that >> is not the case I should think about this when I switch to another >> version as CPython. >> >> I wrote a module where I have: >> def get_indexed_message(message_filename, index): >> """ >> Get index message from a file, where 0 gets the first message >> """ >> >> return open(expanduser(message_filename), >> 'r').readlines()[index].rstrip() >> >> But this can be used by others also and they could be using Jython >> or another implementation. So should I rewrite this and other >> functions? Or would it be OK because the open is in a function? >> > > No, it's not going to close the file just because the open is in a > function. The "with" construct was designed to help solve exactly > this problem. Please use it. I already done it. I thought it not to much work. And it even makes some code shorter: - marshal_file = open(expanduser(marshal_filename), 'r') - not_list = load(marshal_file) - marshal_file.close() - return not_list + with open(expanduser(marshal_filename), 'r') as f: + return load(f) But here I did the close myself already, so that is not completely honest of me. ;-) I should spend some time to make my code more consistent. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof