python 3, subclassing TextIOWrapper.

lambertdw at lambertdw at
Sun Mar 22 03:58:07 CET 2009

    A python 3 question.
    Presume this code is in file
    The program fails.

        $ python3
        ValueError: I/O operation on closed file.

    Removing the comment character to increase the stream
    reference count fixes the program, at the expense of
    an extra TextIOWrapper object.

    Please, what is a better way to write the class with
    regard to this issue?

import re
import io

class file(io.TextIOWrapper):

        Enhance TextIO.  Streams have many sources,
        a file name is insufficient.

    def __init__(self,stream): = stream

    def seek_pattern(self,pattern):
            A motivational method, otherwise inconsequential to the
        search = re.compile(pattern).search
        while True:
            line = next(self)
            if (not line) or search(line):
                return line


More information about the Python-list mailing list