[Tutor] How to open the closed file again?

Alan Plum alan.plum at uni-koeln.de
Tue Jan 5 11:34:52 CET 2010

On Di, 2010-01-05 at 16:24 +0800, 朱淳 wrote:
> I found that after closing files some closed files were left in the
> list:

Why don't you use a dictionary mapping filenames to open file objects

files = {'/path/to/file1': None, '/path/to/file2': None}

def openFiles():
    for f in files.keys():
        if files[f] is not None:
        files[f] = open(f)

def closeFiles():
    for f in files.keys():
        if files[f] is None:
        files[f] = None

Although you'll probably want to refactor this into an object.


Alan Plum

More information about the Tutor mailing list