Skip to main content

Temporary Version Build Coverage Health

Compatibility Implementations Format Downloads

Context managers for managing temporary files and directories.

with temporary.temp_dir() as d:
    ...

with temporary.temp_file(content='hello') as f:
    ...

Installation:

$ pip install temporary

Temporary Directory Examples

The temporary directory is created when entering the context manager, and deleted when exiting it:

>>> import temporary
>>> with temporary.temp_dir() as temp_dir:
...     assert temp_dir.is_dir()
>>> assert not temp_dir.exists()

This time let’s make the temporary directory our working directory:

>>> import os
>>> with temporary.temp_dir(make_cwd=True) as temp_dir:
...     assert str(temp_dir) == os.getcwd()
>>> assert not str(temp_dir) == os.getcwd()

The suffix, prefix, and parent_dir options are passed to the standard tempfile.mkdtemp() function:

>>> with temporary.temp_dir() as p:
...     with temporary.temp_dir(suffix='suf', prefix='pre', parent_dir=p) as d:
...         assert d.parent == p
...         assert d.name.startswith('pre')
...         assert d.name.endswith('suf')

This function can also be used as a decorator, with the in_temp_dir alias:

>>> @temporary.in_temp_dir()
... def my_function():
...     assert old_cwd != os.getcwd()
...
>>> old_cwd = os.getcwd()
>>> my_function()
>>> assert old_cwd == os.getcwd()

Temporary File Examples

The temporary file is created when entering the context manager and deleted when exiting it.

>>> import temporary
>>> with temporary.temp_file() as temp_file:
...     assert temp_file.exists()
>>> assert not temp_file.exists()

The user may also supply some content for the file to be populated with:

>>> with temporary.temp_file('hello!') as temp_file:
...     with temp_file.open() as f:
...         assert f.read() == 'hello!'

The temporary file can be placed in a custom directory:

>>> with temporary.temp_dir() as temp_dir:
...     with temporary.temp_file(parent_dir=temp_dir) as temp_file:
...         assert temp_file.parent == temp_dir

If, for some reason, the user wants to delete the temporary file before exiting the context, that’s okay too:

>>> with temporary.temp_file() as temp_file:
...     temp_file.unlink()

Metadata

Release files for temporary 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for temporary 3.0.0
File Size Uploaded
temporary-3.0.0.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for temporary 3.0.0
File Interpreter ABI Platform
temporary-3.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 7.2 kB

Release files / temporary-3.0.0.tar.gz

Download URL temporary-3.0.0.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6a56e5cc0b5df7c1f55cefefba9fb5f71cc13332b2929f2c8976db9be3bed773
BLAKE2b-256 checksum
How to use checksums
e30709d77128298b67b5cd7a444604e393b43ccf32d19090b0167c13bb829328
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / temporary-3.0.0-py2.py3-none-any.whl

Download URL temporary-3.0.0-py2.py3-none-any.whl
Size 4.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
4d83707f978b7817e20f38fd967a68d3373786fa6ffd85868744cf0c08cb8199
BLAKE2b-256 checksum
How to use checksums
bd7d418a93dcb56844cfb7aa500716716891d360d8e2b364705b9f55f4622ee8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 release files

2.0.1

2 release files

2.0.0

1 release file

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page