Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a directory created with tempfile.mkdtemp?

Tags:

python

linux

I have a python program that creates temporary directories under /temp by using tempfile.mkdtemp. Unfortunately, the Python program did not delete the directory after using it. So now the disk space is low.

Questions:

  1. How do I delete the temporary directories left under /temp manually? I tried to delete them manually but got "permission denied" error.
  2. In the Python program, how to delete temp directory after using them?
like image 784
Yesheng Li Avatar asked Jul 30 '11 18:07

Yesheng Li


People also ask

How do you delete a directory in Python?

Deleting file/dir using the os.rmdir() method in Python is used to remove or delete an empty directory. OSError will be raised if the specified path is not an empty directory.

How do I delete temp files in Python?

remove(os. path. join(temp_dir, now+". zip")) and you should use it whenever the file is to be deleted.

Are temp files automatically deleted?

Most of the temporary files that the system uses are deleted automatically after the task is complete. But there can be some files which stay in your storage for future use. The same can apply for your daily use programs which need these temporary files to complete operations and tasks faster for the users.

Where is my python TEMP folder?

This name is generally obtained from tempdir environment variable. On Windows platform, it is generally either user/AppData/Local/Temp or windowsdir/temp or systemdrive/temp. On linux it normally is /tmp. This directory is used as default value of dir parameter.


2 Answers

To manage resources (like files) in Python, best practice is to use the with keyword, which automatically releases the resources (i.e., cleans up, like closing files); this is available from Python 2.5.

From Python 3.2, you can use tempfile.TemporaryDirectory() instead of tempfile.mkdtmp() – this is usable in with and automatically cleans up the directory:

from tempfile import TemporaryDirectory  with TemporaryDirectory() as temp_dir:     # ... do something with temp_dir # automatically cleaned up when context exited 

If you are using an earlier version of Python (at least 2.5, so have with), you can use backports.tempfile; see Nicholas Bishop’s answer to tempfile.TemporaryDirectory context manager in Python 2.7.

It’s easy and instructive to roll your own class, called a context manager. The return value of the __enter__() method is bound to the target of the as clause, while the __exit__() method is called when the context is exited – even by exception – and performs cleanup.

import shutil import tempfile  class TemporaryDirectory(object):     """Context manager for tempfile.mkdtemp() so it's usable with "with" statement."""     def __enter__(self):         self.name = tempfile.mkdtemp()         return self.name      def __exit__(self, exc_type, exc_value, traceback):         shutil.rmtree(self.name) 

You can simplify this with the @contextlib.contextmanager decorator, so you don’t need to write a context manager manually. The code prior to the yield is executed when entering the context, the yielded value is bound to the target of the as, and the code after the yield is executed when exiting the context. This is fundamentally a coroutine that encapsulates the resource acquisition and release, with the yield yielding control to the suite (body) of the with clause. Note that here you do need to have a try...finally block, as @contextlib.contextmanager does not catch exceptions in the yield – this just factors the resource management into a coroutine.

from contextlib import contextmanager import tempfile import shutil  @contextmanager def TemporaryDirectory():     name = tempfile.mkdtemp()     try:         yield name     finally:         shutil.rmtree(name) 

As simplylizz notes, if you don’t mind the directory already being deleted (which the above code assumes does not happen), you can catch the “No such file or directory” exception as follows:

import errno # ... try:     shutil.rmtree(self.name) except OSError as e:     # Reraise unless ENOENT: No such file or directory     # (ok if directory has already been deleted)     if e.errno != errno.ENOENT:         raise 

You can compare with the standard implementation in tempfile.py; even this simple class has had bugs and evolved over the years.

For background on with, see:

  • The Python Tutorial: Methods of File Objects
  • With Statement Context Managers
  • PEP 343 -- The "with" Statement
like image 179
Nils von Barth Avatar answered Sep 20 '22 11:09

Nils von Barth


Read the documentation, it's simple. ;) From the docs: the directory is readable, writable, and searchable only by the creating user ID.

To delete temp directory try something like this:

import errno import shutil import tempfile  try:     tmp_dir = tempfile.mkdtemp()  # create dir     # ... do something finally:     try:         shutil.rmtree(tmp_dir)  # delete directory     except OSError as exc:         if exc.errno != errno.ENOENT:  # ENOENT - no such file or directory             raise  # re-raise exception 

Also you can try tempdir package or see its sources.

like image 28
simplylizz Avatar answered Sep 17 '22 11:09

simplylizz