Using NamedTemporaryDir instead of multiple NamedTemporaryFiles

  • Thread starter Michael Hoffman
  • Start date
M

Michael Hoffman

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
 
M

Michael Hoffman

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.
 
G

Gabriel Genellina

En Tue, 09 Sep 2008 15:49:32 -0300, Michael Hoffman
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top