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'):
>
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
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
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 :
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
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
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"
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
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
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
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
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
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
13 matches
Mail list logo