Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > file.close()

Reply
Thread Tools

file.close()

 
 
Ben Finney
Guest
Posts: n/a
 
      07-24-2003
On Wed, 23 Jul 2003 20:29:27 -0700, Erik Max Francis wrote:
> Bryan wrote:
>> is what i'm doing is overkill?

>
> Use of external resources -- which should be released as soon as
> you're done with them -- are best done in try/finally clauses.


It seems that nesting the 'try' clauses doesn't scale well. What if
fifty files are opened? Must the nesting level of the 'try' clauses be
fifty also, to close them promptly?

--
\ "God forbid that any book should be banned. The practice is as |
`\ indefensible as infanticide." -- Dame Rebecca West |
_o__) |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
 
Reply With Quote
 
 
 
 
Bryan
Guest
Posts: n/a
 
      07-24-2003
i'm curious to know how others handle the closing of files. i seem to
always end up with this pattern even though i rarely see others do it.

f1 = file('file1')
try:
# process f1
finally:
f1.close()

which explicitly closes f1

or for multiple files:

f1 = file('file1')
try:
f2 = file('file2')
try:
# process f1 & f2
finally:
f2.close()
finally:
f1.close()

which explicitly closes f1 & f2
any exceptions opening f1 or f2 is handled outside of this structure or is
allowed to fall out of the program. i'm aware that files will automatically
be closed when the process exits. i'm just curious how others do this for
small scripts and larger programs. is what i'm doing is overkill?

thanks,

bryan


 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      07-24-2003
Bryan wrote:

> which explicitly closes f1 & f2
> any exceptions opening f1 or f2 is handled outside of this structure
> or is
> allowed to fall out of the program. i'm aware that files will
> automatically
> be closed when the process exits. i'm just curious how others do this
> for
> small scripts and larger programs. is what i'm doing is overkill?


No, not at all; it's safe and portable. Python the language does not
specify when objects get reclaimed, although CPython the implementation
does it promptly. Use of external resources -- which should be released
as soon as you're done with them -- are best done in try/finally
clauses.

--
Erik Max Francis && http://www.velocityreviews.com/forums/(E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ It is fatal to enter any war without the will to win it.
\__/ Douglas MacArthur
 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      07-24-2003
Bryan (original poster) wrote:
> f1 = file('file1')
> try:
> f2 = file('file2')
> try:
> # process f1 & f2
> finally:
> f2.close()
> finally:
> f1.close()



On Wed, 23 Jul 2003 21:12:34 -0700, Erik Max Francis wrote:
> Ben Finney wrote:
>> It seems that nesting the 'try' clauses doesn't scale well. What if
>> fifty files are opened? Must the nesting level of the 'try' clauses
>> be fifty also, to close them promptly?

>
> If you're manipulating fifty files in one block, presumably you're
> doing so in a uniform way:
>
> allFiles = [...]
> try:
> ...
> finally:
> for eachFile in allFiles:
> eachFile.close()


This doesn't match Bryan's nested structure above, which you blessed as
not "overkill" (in his words). It was this that I considered a
poorly-scaling structure, or "overkill" since only one 'try' block is
required. Do you disagree?

--
\ "Too many Indians spoil the golden egg." -- Sir Joh |
`\ Bjelke-Petersen |
_o__) |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      07-24-2003
Ben Finney wrote:

> It seems that nesting the 'try' clauses doesn't scale well. What if
> fifty files are opened? Must the nesting level of the 'try' clauses
> be
> fifty also, to close them promptly?


If you're manipulating fifty files in one block, presumably you're doing
so in a uniform way:

allFiles = [...]
try:
...
finally:
for eachFile in allFiles:
eachFile.close()

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Man is a hating rather than a loving animal.
\__/ Rebecca West
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      07-24-2003
Ben Finney wrote:
> On Wed, 23 Jul 2003 20:29:27 -0700, Erik Max Francis wrote:
>> Bryan wrote:
>>> is what i'm doing is overkill?

>>
>> Use of external resources -- which should be released as soon as
>> you're done with them -- are best done in try/finally clauses.

>
> It seems that nesting the 'try' clauses doesn't scale well. What if
> fifty files are opened? Must the nesting level of the 'try' clauses be
> fifty also, to close them promptly?


If you need 50 open files, you almost certainly want to have a way of
organizing them. Probably they'll be in a list or dictionary. So, if
they're in a list, for example, you can do this:


filelist = []
try:
filelist.append(open(filename[0]))
filelist.append(open(filename[1]))
...
do_something(filelist)
finally:
for f in filelist:
f.close()


--
CARL BANKS
 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      07-24-2003
Ben Finney wrote:

> This doesn't match Bryan's nested structure above, which you blessed
> as
> not "overkill" (in his words). It was this that I considered a
> poorly-scaling structure, or "overkill" since only one 'try' block is
> required. Do you disagree?


It uses try/finally to secure the closing of many files in a timely
manner. In that sense, it certainly fits the pattern. It doesn't have
the same nested pattern, but try/finally isn't at issue here. If you
had code which opened 50 files and looked like:

fileOne = file(...)
fileTwo = file(...)
fileThree = file(...)
...
fileFortyNine = file(...)
fileFifty = file(...)

I would say you are doing something wrong. A more systematic handling
of many, many files is indicated whether or not you're using the
try/finally idiom to ensure files get closed in a timely manner.

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Together we can take this one day at a time
\__/ Sweetbox
 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      07-24-2003
>
> If you need 50 open files, you almost certainly want to have a way of
> organizing them. Probably they'll be in a list or dictionary. So, if
> they're in a list, for example, you can do this:
>
>
> filelist = []
> try:
> filelist.append(open(filename[0]))
> filelist.append(open(filename[1]))
> ...
> do_something(filelist)
> finally:
> for f in filelist:
> f.close()
>



erik, carl... thanks... this is exactly what i was looking for

bryan


 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      07-24-2003
On Thu, 24 Jul 2003 05:20:15 GMT, Bryan wrote:
>> filelist = []
>> try:
>> filelist.append(open(filename[0]))
>> filelist.append(open(filename[1]))
>> ...
>> do_something(filelist)
>> finally:
>> for f in filelist:
>> f.close()

>
> erik, carl... thanks... this is exactly what i was looking for


The only substantial difference I see between this and what you
originally posted, is that there is only one 'try...finally' block for
all the file open/close operations. Is this what you were wanting
clarified?

--
\ "I spent all my money on a FAX machine. Now I can only FAX |
`\ collect." -- Steven Wright |
_o__) |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      07-24-2003
On Wed, 23 Jul 2003 22:19:07 -0700, Erik Max Francis wrote:
> Ben Finney wrote:
>> This doesn't match Bryan's nested structure above, which you blessed
>> as not "overkill" (in his words).

> It doesn't have the same nested pattern, but try/finally isn't at
> issue here.


Judging by Bryan's responses elsewhere in this thread, the multiple
nested 'try...finally' is indeed what he was asking about. The question
seems to be answered now.

--
\ "Those who will not reason, are bigots, those who cannot, are |
`\ fools, and those who dare not, are slaves." -- "Lord" George |
_o__) Gordon Noel Byron |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments