Hi all,<br> I put files in a list, just like this module:<br> <br>#! /usr/bin/python<br># -*- coding=utf-8 -*-<br><br><br>fileList = []<br><br>def openFiles():<br> for i in range(0,2):<br> fname = "file%s"%i<br>
f = open(fname,"a")<br> fileList.append(f)<br> <br>def closeFiles():<br> for f in fileList:<br> f.close()<br> <br>if __name__=="__main__":<br> openFiles()<br> print "fileList<br>
closeFiles()<br> print fileList<br> openFiles()<br> print fileList<br><br> I found that after closing files some closed files were left in the list:<br><br>open files:[<open file 'file0', mode 'a' at 0xb7be1430>, <open file 'file1', mode 'a' at 0xb7be1480>]<br>
close files:[<closed file 'file0', mode 'a' at 0xb7be1430>, <closed file 'file1', mode 'a' at 0xb7be1480>]<br>open files again:[<closed file 'file0', mode 'a' at 0xb7be1430>, <closed file 'file1', mode 'a' at 0xb7be1480>, <open file 'file0', mode 'a' at 0xb7be14d0>, <open file 'file1', mode 'a' at 0xb7be1520>]<br>
<br> After I call openFiles() and closeFiles() many times, the list will become fatter and fatter, filled with valueless closed file object. How could I open the closed file? I can't find the way in python document.<br>