Менеджер контекста tempfile.TemporaryDirectory в Python 2.7

Есть ли способ создать временный каталог в контекстном менеджере с Python 2.7?

with tempfile.TemporaryDirectory() as temp_dir:
    # modify files in this dir

# here the temporary diretory does not exist any more.

person guettli    schedule 10.10.2013    source источник
comment
См. stackoverflow.com/q/6884991, чтобы узнать, как это сделать вручную.   -  person Nils von Barth    schedule 02.11.2017


Ответы (2)


Другим вариантом является пакет «backports.tempfile» на pypi: https://pypi.python.org/pypi/backports.tempfile

Цитируя описание проекта: «Этот пакет предоставляет резервные копии новых функций в модуле tempfile Python в пространстве имен backports».

Установить с помощью:

pip install backports.tempfile

Затем используйте его в своем скрипте:

from backports import tempfile
with tempfile.TemporaryDirectory() as temp_dir:
    # modify files in this dir
# here the temporary directory does not exist any more.
person Nicholas Bishop    schedule 22.09.2016
comment
Мне это нравится больше, чем копирование + вставка фрагментов. Спасибо. Я надеюсь, что мой вопрос не получит близкого голосования, так как я предпочитаю библиотеки. С вопросами о библиотеках следует обращаться на softwarerecs.stackexchange.com... Кстати: я шучу, я не понимаю, почему сайт sofwarerecs существуют. Почему бы не задать вопросы о рекомендациях по программному обеспечению на StackOverFlow... - person guettli; 22.09.2016
comment
Досадно, что бэкпорт не использует псевдонимы всех существующих tempfile вещей, поэтому вам может понадобиться и то, и другое. - person Nick T; 21.06.2017

tempfile.TemporaryDirectory() добавлен в стандартную библиотеку tempfile в Python 3.2.

Это простая оболочка для tempfile.mkdtemp. Он написан на чистом Python и может быть легко перенесен на Python 2.7.

Например:

from __future__ import print_function

import warnings as _warnings
import os as _os

from tempfile import mkdtemp

class TemporaryDirectory(object):
    """Create and return a temporary directory.  This has the same
    behavior as mkdtemp but can be used as a context manager.  For
    example:

        with TemporaryDirectory() as tmpdir:
            ...

    Upon exiting the context, the directory and everything contained
    in it are removed.
    """

    def __init__(self, suffix="", prefix="tmp", dir=None):
        self._closed = False
        self.name = None # Handle mkdtemp raising an exception
        self.name = mkdtemp(suffix, prefix, dir)

    def __repr__(self):
        return "<{} {!r}>".format(self.__class__.__name__, self.name)

    def __enter__(self):
        return self.name

    def cleanup(self, _warn=False):
        if self.name and not self._closed:
            try:
                self._rmtree(self.name)
            except (TypeError, AttributeError) as ex:
                # Issue #10188: Emit a warning on stderr
                # if the directory could not be cleaned
                # up due to missing globals
                if "None" not in str(ex):
                    raise
                print("ERROR: {!r} while cleaning up {!r}".format(ex, self,),
                      file=_sys.stderr)
                return
            self._closed = True
            if _warn:
                self._warn("Implicitly cleaning up {!r}".format(self),
                           ResourceWarning)

    def __exit__(self, exc, value, tb):
        self.cleanup()

    def __del__(self):
        # Issue a ResourceWarning if implicit cleanup needed
        self.cleanup(_warn=True)

    # XXX (ncoghlan): The following code attempts to make
    # this class tolerant of the module nulling out process
    # that happens during CPython interpreter shutdown
    # Alas, it doesn't actually manage it. See issue #10188
    _listdir = staticmethod(_os.listdir)
    _path_join = staticmethod(_os.path.join)
    _isdir = staticmethod(_os.path.isdir)
    _islink = staticmethod(_os.path.islink)
    _remove = staticmethod(_os.remove)
    _rmdir = staticmethod(_os.rmdir)
    _warn = _warnings.warn

    def _rmtree(self, path):
        # Essentially a stripped down version of shutil.rmtree.  We can't
        # use globals because they may be None'ed out at shutdown.
        for name in self._listdir(path):
            fullname = self._path_join(path, name)
            try:
                isdir = self._isdir(fullname) and not self._islink(fullname)
            except OSError:
                isdir = False
            if isdir:
                self._rmtree(fullname)
            else:
                try:
                    self._remove(fullname)
                except OSError:
                    pass
        try:
            self._rmdir(path)
        except OSError:
            pass

import os
with TemporaryDirectory() as tmp_dir:
    print("Temporary directory path: %s" % tmp_dir)
    print(os.path.isdir(tmp_dir))

# here the temporary diretory does not exist any more.
print(os.path.isdir(tmp_dir))
person Leonardo.Z    schedule 10.10.2013
comment
Если линии _<thing> = staticmethod(thing) не работают, возможно, их следует удалить? - person Eli Rose; 01.04.2016
comment
import sys as _sys не хватает для print(... file=_sys.stderr)? - person hooblei; 13.10.2016
comment
Ссылка на источник: tempfile.py. Сложный код связан с также пытается очистить во время финализации, если диспетчер контекста не был должным образом закрыт при завершении работы. Это привело к ошибке № 22427 и было упрощено. - person Nils von Barth; 02.11.2017