FAQ
I am writing a library that creates temporary files and calls a series
of external programs to process these files. Sometimes these external
programs create files in the same directory as the input files, so to
make sure they are all deleted, one must create them in a temporary
directory, then delete it.

I've written a NamedTemporaryDir class which is derived somewhat from
tempfile.NamedTemporaryFile in the standard library. Right now I am
using NamedTemporaryFile to create individual files, but since I am
putting them in a directory that will be deleted anyway, I'm wondering
if I can simplify things (and not have to keep track of all fo the
NamedTemporaryFile instances) by using tempfile.mkstemp() specifying my
temporary directory, and relying on the directory deletion when exiting
its with block.

Is there any reason I should keep track of each temporary files myself
instead of deleting the whole directory?

I am using Linux, but I would also be interested in cross-platform
considerations.

Also, the code is below. Is this worth submitting as a patch?

# NamedTemporaryFile is based somewhat on Python 2.5.2
# tempfile._TemporaryFileWrapper
#
# Original Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 Python
# Software Foundation; All Rights Reserved
#
# License at http://www.python.org/download/releases/2.5.2/license/

from tempfile import mkdtemp

class NamedTemporaryDir(object):
def __init__(self, *args, **kwargs):
self.name = mkdtemp(*args, **kwargs)
self.close_called = False

def __enter__(self):
return self

unlink = os.unlink

def close(self):
if not self.close_called:
self.close_called = True
self.unlink(self.name)

def __del__(self):
self.close()

def __exit__(self, exc, value, tb):
result = self.file.__exit__(exc, value, tb)
self.close()
return result

Search Discussions

  • Michael Hoffman at Sep 9, 2008 at 7:08 pm
    Please accept my apologies if this message was posted several times. My
    newsreader claimed that a timeout error kept the message from being
    posted, but I think it got through.
  • Michael Hoffman at Sep 9, 2008 at 7:14 pm

    Michael Hoffman wrote:

    unlink = os.unlink
    Actually, I need to use shutil.rmtree instead, but you get the idea.
  • Gabriel Genellina at Sep 9, 2008 at 8:04 pm
    En Tue, 09 Sep 2008 15:49:32 -0300, Michael Hoffman
    <4g4trz802 at sneakemail.com> escribi?:
    I've written a NamedTemporaryDir class which is derived somewhat from
    tempfile.NamedTemporaryFile in the standard library. Right now I am
    using NamedTemporaryFile to create individual files, but since I am
    putting them in a directory that will be deleted anyway, I'm wondering
    if I can simplify things (and not have to keep track of all fo the
    NamedTemporaryFile instances) by using tempfile.mkstemp() specifying my
    temporary directory, and relying on the directory deletion when exiting
    its with block.
    Looks fine...
    def close(self):
    if not self.close_called:
    self.close_called = True
    self.unlink(self.name)
    Windows won't let you remove a non-empty directory.
    def __exit__(self, exc, value, tb):
    result = self.file.__exit__(exc, value, tb)
    self.close()
    return result
    self.file?

    --
    Gabriel Genellina

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 9, '08 at 6:49p
activeSep 9, '08 at 8:04p
posts4
users2
websitepython.org

People

Translate

site design / logo © 2022 Grokbase