context_temp
Context Manager wrappers for temp files and directories which remove the file or directory when when the manager goes out of scope.
Installation
$ pip install context_temp
Supports
context_temp has been tested with Python 3.8-3.11
Docs & Source
Docs: http://context_temp.readthedocs.io/en/latest/
Source: https://github.com/cltrudeau/context_temp
Version: 0.11.2
Methods
- context_temp.temp_directory(path=None)
This
Context Manager
creates a temporary directory and yields its path. Upon exit the directory is removed.- Parameters:
path – optional path for the creation of the directory, otherwise defaults to operating system’s default temp file location
Example:
with temp_directory() as td: filename = os.path.join(td, 'foo.txt') with open(filename, 'w') as f: f.write('stuff') # got here? => td is now gone, foo.txt is gone too
- context_temp.temp_file(path=None)
This
Context Manager
creates a temporary file which is readable and writable by the current user and yields its path. Once theContext
exits, the file is removed.- Parameters:
path – optional path for the creation of the file, otherwise defaults to operating system’s default temp file location
Example:
with temp_file() as filename: with open(filename, 'w') as f: f.write('stuff') # got here? => file called "filename" is now gone