monkeypatching NamedTemporaryFile

Jason Lunz lunz at falooley.org
Fri May 26 05:26:31 CEST 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