[Tutor] How to open the closed file again?

朱淳 zhuchunml at gmail.com
Tue Jan 5 09:24:15 CET 2010


Hi all,
    I put files in a list, just like this module:

#! /usr/bin/python
# -*- coding=utf-8 -*-


fileList = []

def openFiles():
    for i in range(0,2):
        fname = "file%s"%i
        f = open(fname,"a")
        fileList.append(f)

def closeFiles():
    for f in fileList:
        f.close()

if __name__=="__main__":
    openFiles()
    print "fileList
    closeFiles()
    print fileList
    openFiles()
    print fileList

    I found that after closing files some closed files were left in the
list:

open files:[<open file 'file0', mode 'a' at 0xb7be1430>, <open file 'file1',
mode 'a' at 0xb7be1480>]
close files:[<closed file 'file0', mode 'a' at 0xb7be1430>, <closed file
'file1', mode 'a' at 0xb7be1480>]
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>]

   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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100105/18feefb1/attachment.htm>


More information about the Tutor mailing list