Python Temporary Files and Directories

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Auto-Deleting Temporary Files and Directories

TemporaryFile, NamedTemporaryFile and TemporaryDirectory are classes that offer temporary file and directory functionality and which, in conjunction with the with

Auto-Deleting Temporary File

Auto-Deleting Temporary Directory

Temporary Files and Directories to be Deleted by the Caller

Temporary File to be Deleted by the Caller

Temporary Directory to be Deleted by the Caller

Create a named temporary file, write in it and return the name. The file is not automatically deleted after close(), the caller will need to delete it:

import tempfile
with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as t:
  t.write(something)
  return t.name