Skip to main content

Context managers for managing temporary files and directories.

Project description

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 os.path
>>> import temporary
>>> with temporary.temp_dir() as d:
...     assert os.path.isdir(d)
>>> assert not os.path.exists(d)

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

>>> import os
>>> with temporary.temp_dir(make_cwd=True) as d:
...     assert d == os.getcwd()
>>> assert d != 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 os.path.dirname(d) == p
...         assert os.path.basename(d).startswith('pre')
...         assert os.path.basename(d).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 Files Examples

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

>>> import os.path
>>> import temporary
>>> with temporary.temp_file() as f_name:
...     assert os.path.isfile(f_name)
>>> assert not os.path.exists(f_name)

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

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

The temporary file can be placed in a custom directory:

>>> with temporary.temp_dir() as d_name:
...     with temporary.temp_file(parent_dir=d_name) as f_name:
...         assert os.path.dirname(f_name) == d_name

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

>>> import os
>>> with temporary.temp_file() as f_name:
...     os.remove(f_name)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

temporary-1.1.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

temporary-1.1.0-py2.py3-none-any.whl (4.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file temporary-1.1.0.tar.gz.

File metadata

  • Download URL: temporary-1.1.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for temporary-1.1.0.tar.gz
Algorithm Hash digest
SHA256 daa2b33a31a39d93012d347149fc8b800c7557567562ff9107436f23a05f7b1c
MD5 28ce194ed12a3a84c04d7042b590c295
BLAKE2b-256 72911cd1636f44d84208ccbf8639f7ae0019d1fac08d9575040544a19a9ec87c

See more details on using hashes here.

File details

Details for the file temporary-1.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for temporary-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b84c19c8978d527bfdd06325562307e9f1f70d85bb9e2d59b3ba56e9c4fe7407
MD5 2b9c1f6cf976c965e2e41ed464597267
BLAKE2b-256 56c776f5048a5099cc8f5f6947f277ad39a80429e8ece47bdc6287136a321478

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page