Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-18 Thread Jeremy Leonard
On Tuesday, February 16, 2016 at 3:39:34 AM UTC-5, jf...@ms4.hinet.net wrote: > I know > > with open('foo.txt') as f: > ...do something... > > will close the file automatically when the "with" block ends. > > I also saw codes in a book: > > for line in open('foo.txt'): >

Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-17 Thread Tim Chase
On 2016-02-17 16:51, Steven D'Aprano wrote: > If you want the file to be closed immediately, you must: > > - use a with statement; > > - or explicitly call f.close() I have a lot of pre-"with" code (i.e., Py2.4) that looks like f = open(...) try: do_stuff() finally: f.close() To

Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-17 Thread Cameron Simpson
On 18Feb2016 02:05, Mark Lawrence wrote: On 18/02/2016 01:29, jf...@ms4.hinet.net wrote: The "for ... open ..." is definitely not a good design pattern. It opens a file at "for" block but leaves it closed somewhere in the sky. Hardly, as all ready explained, but

Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-17 Thread Mark Lawrence
On 18/02/2016 01:29, jf...@ms4.hinet.net wrote: The "for ... open ..." is definitely not a good design pattern. It opens a file at "for" block but leaves it closed somewhere in the sky. Hardly, as all ready explained, but how about this handle = open('foo.txt') for line in handle :

Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-17 Thread jfong
The "for ... open ..." is definitely not a good design pattern. It opens a file at "for" block but leaves it closed somewhere in the sky. > The garbage collector will: > - reclaim the memory used by the object; > - close the file. I suppose (IMO) that the primitive idea of garbage collection

Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-16 Thread Steven D'Aprano
On Wednesday 17 February 2016 15:04, jf...@ms4.hinet.net wrote: > Thanks for these detailed explanation. Both statements will close file > automatically sooner or later and, when considering the exceptions, "with" > is better. Hope my understanding is right. > > But, just curious, how do you

Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-16 Thread Raspberry Aether
On 02/16/2016 11:04 PM, jf...@ms4.hinet.net wrote: > Thanks for these detailed explanation. Both statements will close file > automatically sooner or later and, when considering the exceptions, "with" is > better. Hope my understanding is right. > > But, just curious, how do you know the "for"

Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-16 Thread Chris Angelico
On Wed, Feb 17, 2016 at 3:04 PM, wrote: > Thanks for these detailed explanation. Both statements will close file > automatically sooner or later and, when considering the exceptions, "with" is > better. Hope my understanding is right. > > But, just curious, how do you know

Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-16 Thread jfong
Thanks for these detailed explanation. Both statements will close file automatically sooner or later and, when considering the exceptions, "with" is better. Hope my understanding is right. But, just curious, how do you know the "for" will do it? I can't find any document about it from every

Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-16 Thread Terry Reedy
On 2/16/2016 3:39 AM, jf...@ms4.hinet.net wrote: I know with open('foo.txt') as f: ...do something... will close the file automatically when the "with" block ends. I also saw codes in a book: for line in open('foo.txt'): ...do something... Some books were

Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-16 Thread Cameron Simpson
On 16Feb2016 00:39, jf...@ms4.hinet.net wrote: I know with open('foo.txt') as f: ...do something... will close the file automatically when the "with" block ends. Yes, because open is a context manager - they're great for reliably tidying up in the face of

Re: Will file be closed automatically in a "for ... in open..." statement?

2016-02-16 Thread Chris Angelico
On Tue, Feb 16, 2016 at 7:39 PM, wrote: > I know > > with open('foo.txt') as f: > ...do something... > > will close the file automatically when the "with" block ends. > > I also saw codes in a book: > > for line in open('foo.txt'): > ...do

Will file be closed automatically in a "for ... in open..." statement?

2016-02-16 Thread jfong
I know with open('foo.txt') as f: ...do something... will close the file automatically when the "with" block ends. I also saw codes in a book: for line in open('foo.txt'): ...do something... but it didn't mention if the file will be closed automatically or not when