monkeypatching NamedTemporaryFile

Jason Lunz lunz at falooley.org
Thu May 25 23:26:31 EDT 2006


Is there a better way to do this?

def QuietNamedTemporaryFile(**kwargs):

    '''
    Return a NamedTemporaryFile that doesn't complain when its file has already
    been unlinked at __del__ time.
    '''

    tf = tempfile.NamedTemporaryFile(**kwargs)

    def quiet_del():
        try:
            tf.close()
        except OSError:
            pass

    tf.__del__ = quiet_del

    return tf

Jason




More information about the Python-list mailing list