Message149393
I think that the fact that Nick got the code to close multiple files wrong underlines that it is difficult to get right currently. Nick's code
try:
files = [open(fname) for fname in names]
# ...
finally:
for f in files:
f.close()
only closes the files if all of them were opened successfully. Moreover, `file.close()` can fail for various reasons, which would result in all remaining files being left open. When we fix both problems, the code becomes
try:
files = []
for fname in names:
files.append(open(fname))
# ...
finally:
for f in files:
try:
f.close()
except IOError:
pass
I think everyone will agree that the version using 'CleanupManager' is nicer. To be fair, we should note that the need to open many files simultaneously is not very common -- usually, we can make to with opening the files one by one. |
|
Date |
User |
Action |
Args |
2011-12-13 15:41:48 | smarnach | set | recipients:
+ smarnach, rhettinger, ncoghlan, giampaolo.rodola, nikratio, Julian, eric.snow |
2011-12-13 15:41:48 | smarnach | set | messageid: <1323790908.16.0.612869623142.issue13585@psf.upfronthosting.co.za> |
2011-12-13 15:41:47 | smarnach | link | issue13585 messages |
2011-12-13 15:41:47 | smarnach | create | |
|