Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Opening files without closing them (http://www.velocityreviews.com/forums/t355129-opening-files-without-closing-them.html)

Sandra-24 03-05-2006 10:33 PM

Opening files without closing them
 
I was reading over some python code recently, and I saw something like
this:

contents = open(file).read()

And of course you can also do:

open(file, "w").write(obj)

Why do they no close the files? Is this sloppy programming or is the
file automatically closed when the reference is destroyed (after this
line)? I usually use:

try:
f = open(file)
contents = f.read()
finally:
f.close()

But now I am wondering if that is the same thing. Which method would
you rather use? Why?

Thanks,
Sandra


=?UTF-8?B?TWFyY2luIE1pZWzFvHnFhHNraQ==?= 03-05-2006 10:44 PM

Re: Opening files without closing them
 
Sandra-24 wrote:
> I was reading over some python code recently, and I saw something like
> this:
>
> contents = open(file).read()
>
> And of course you can also do:
>
> open(file, "w").write(obj)
>
> Why do they no close the files? Is this sloppy programming or is the
> file automatically closed when the reference is destroyed (after this
> line)? I usually use:
>
> try:
> f = open(file)
> contents = f.read()
> finally:
> f.close()
>


this above is equivalent to:

open(file){|f|
contents=f.read
}

the logic taking care of everything is encapsulated in open.

but can be done in less ruby way way :)



lopex

Robert Kern 03-05-2006 10:44 PM

Re: Opening files without closing them
 
Sandra-24 wrote:
> I was reading over some python code recently, and I saw something like
> this:
>
> contents = open(file).read()
>
> And of course you can also do:
>
> open(file, "w").write(obj)
>
> Why do they no close the files? Is this sloppy programming or is the
> file automatically closed when the reference is destroyed (after this
> line)?


Both!

Usually, the files will probably be closed *if* you are using CPython. However,
there is absolutely no guarantee of this behavior. For example, Jython uses a
different garbage collection scheme, and the files will *not* close immediately.
Future versions of CPython may have different behavior, too.

> I usually use:
>
> try:
> f = open(file)
> contents = f.read()
> finally:
> f.close()
>
> But now I am wondering if that is the same thing. Which method would
> you rather use? Why?


Just keep doing what you are doing, please.

--
Robert Kern
robert.kern@gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco


=?UTF-8?B?TWFyY2luIE1pZWzFvHnFhHNraQ==?= 03-05-2006 10:45 PM

Re: Opening files without closing them
 
Marcin Mielżyński wrote:
> Sandra-24 wrote:
>> I was reading over some python code recently, and I saw something like
>> this:
>>
>> contents = open(file).read()
>>
>> And of course you can also do:
>>
>> open(file, "w").write(obj)
>>
>> Why do they no close the files? Is this sloppy programming or is the
>> file automatically closed when the reference is destroyed (after this
>> line)? I usually use:
>>
>> try:
>> f = open(file)
>> contents = f.read()
>> finally:
>> f.close()
>>

>
> this above is equivalent to:
>
> open(file){|f|
> contents=f.read
> }
>
> the logic taking care of everything is encapsulated in open.
>
> but can be done in less ruby way way :)
>
>
>
> lopex


Oops I thought I was writing to c.l.ruby :D

sorry for spam

lopex

Erik Max Francis 03-05-2006 11:59 PM

Re: Opening files without closing them
 
Robert Kern wrote:

>> I usually use:
>>
>> try:
>> f = open(file)
>> contents = f.read()
>> finally:
>> f.close()
>>
>> But now I am wondering if that is the same thing. Which method would
>> you rather use? Why?

>
> Just keep doing what you are doing, please.


Note quite. The assignment of the resources to its variable needs to be
done before the try:

f = open(file)
try:
contents = f.read()
finally:
f.close()

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Who, my friend, can scale heaven?
-- _Gilgamesh_, ca. 3rd C. BC

Robert Kern 03-06-2006 12:04 AM

Re: Opening files without closing them
 
Erik Max Francis wrote:
> Robert Kern wrote:
>
>>>I usually use:
>>>
>>>try:
>>> f = open(file)
>>> contents = f.read()
>>>finally:
>>> f.close()
>>>
>>>But now I am wondering if that is the same thing. Which method would
>>>you rather use? Why?

>>
>>Just keep doing what you are doing, please.

>
> Note quite. The assignment of the resources to its variable needs to be
> done before the try:
>
> f = open(file)
> try:
> contents = f.read()
> finally:
> f.close()


Yes, you are correct.

--
Robert Kern
robert.kern@gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco


Steven Bethard 03-06-2006 12:36 AM

Re: Opening files without closing them
 
Sandra-24 wrote:
> I was reading over some python code recently, and I saw something like
> this:
>
> contents = open(file).read()
>
> And of course you can also do:
>
> open(file, "w").write(obj)
>
> Why do they no close the files? Is this sloppy programming or is the
> file automatically closed when the reference is destroyed (after this
> line)? I usually use:
>
> try:
> f = open(file)
> contents = f.read()
> finally:
> f.close()
>
> But now I am wondering if that is the same thing. Which method would
> you rather use? Why?


In Python 2.5, you'll write::

with open(file) as f:
contents = f.read()

and Python will automatically close the file at the end of the
with-statement. Observe:

Python 2.5a0 (trunk:42857M, Mar 5 2006, 14:50:28) [MSC v.1310 32 bit
(Intel)] on win32
>>> from __future__ import with_statement
>>> with open('readme.txt') as f:

.... contents = f.read()
....
>>> f

<closed file 'readme.txt', mode 'r' at 0x00B8BAA8>

Of course, you have to wait until August or so for Python 2.5:
http://www.python.org/peps/pep-0356.html

STeVe

Bruno Desthuilliers 03-06-2006 02:03 AM

Re: Opening files without closing them
 
Sandra-24 a crit :
> I was reading over some python code recently, and I saw something like
> this:
>
> contents = open(file).read()
>
> And of course you can also do:
>
> open(file, "w").write(obj)
>
> Why do they no close the files? Is this sloppy programming or is the
> file automatically closed when the reference is destroyed (after this
> line)?


IIRC, the current CPython implementation takes care of closing file
objects that are no longer referenced. But this may not be true of other
implementations (think: Jython).

> I usually use:
>
> try:
> f = open(file)
> contents = f.read()
> finally:
> f.close()
>
> But now I am wondering if that is the same thing.


Not quite:

>>> try:

.... f = open('file_that_doesnt_exists.ext')
.... finally:
.... f.close()
....
Traceback (most recent call last):
File "<stdin>", line 4, in ?
NameError: name 'f' is not defined
>>>


> Which method would
> you rather use?


For a quick script, the simplest one. For production code, it depends
too much on the context to give a definitive single answer.

3c273 03-06-2006 09:47 PM

Re: Opening files without closing them
 
"Erik Max Francis" <max@alcyone.com> wrote in message
news:wPCdnSJq7ObP5pbZnZ2dnUVZ_sSdnZ2d@speakeasy.ne t...
> Note quite. The assignment of the resources to its variable needs to be
> done before the try:
>
> f = open(file)
> try:
> contents = f.read()
> finally:
> f.close()
>

Pardon the newbie question, but could you explain why? I have been doing it
the same way as the OP and would like to know the difference. Thank you.
Louis



Paul Rubin 03-06-2006 10:14 PM

Re: Opening files without closing them
 
"3c273" <nospam@nospam.com> writes:
> > f = open(file)
> > try:
> > contents = f.read()
> > finally:
> > f.close()
> >

> Pardon the newbie question, but could you explain why? I have been doing it
> the same way as the OP and would like to know the difference. Thank you.


Say that the open is inside the try block. If the file can't be
opened, then 'open' raises an exception, 'f' doesn't get set, and then
the 'finally' clause tries to close f. f might have been previously
bound to some other file (which still has other handles alive) and so
the wrong file gets closed.


All times are GMT. The time now is 08:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.