file.close()

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Thu Jul 24 00:06:00 EDT 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




More information about the Python-list mailing list