Create self-cleaning ephemeral directories that vanish after a lifetime or on restart.
Project description
ephemdir
ephemdir (ephemeral directory) creates temporary directories that clean
themselves up — automatically removed once their lifetime expires or after the
machine restarts. Names are playful two-word slugs like brave-otter or
nimble-marmot instead of dull ones like tmp_data.
Works on Linux, macOS and Windows with a single small dependency
(coolname).
Installation
pip install ephemdir
Quick start
from ephemdir import tempdir
# Lives until the next system restart (the default).
work = tempdir()
print(work) # -> /current/dir/brave-otter
(work.path / "data.txt").write_text("hello")
# Also expires after a given lifetime.
cache = tempdir(lifetime="2h") # "30m", "1h30m", 3600, timedelta(hours=2) ...
# Survive restarts and rely only on the lifetime.
keep = tempdir(lifetime="7d", remove_on_restart=False)
# Do not delete while a process still has files open inside (Linux/macOS).
busy = tempdir(lifetime="1h", keep_while_in_use=True)
# Use as a context manager: removed automatically when the block exits.
with tempdir() as scratch:
(scratch / "tmp.bin").write_bytes(b"...")
tempdir() parameters
| Parameter | Default | Description |
|---|---|---|
lifetime |
None |
Time to live: seconds, timedelta, or "2h"/"1h30m". None = until restart. |
remove_on_restart |
True |
Remove the directory after the machine reboots. |
keep_while_in_use |
False |
Defer deletion while files are still open inside (Linux/macOS). |
parent |
current dir | Where to create the directory. |
prefix |
"" |
Prefix prepended to the generated name. |
words |
2 |
Number of words in the generated name. |
Any option left unset falls back to the user config file, then to the built-in default shown above.
Managing a directory
d = tempdir()
d.path # pathlib.Path to the directory
d.created_at # creation timestamp
d.expires_at # expiry timestamp or None
d.remove() # delete now and stop tracking
d.keep() # stop tracking but keep on disk (becomes permanent)
How cleanup works
ephemdir records each directory in a small JSON registry in your per-user data directory. Cleanup happens in two ways:
- Lazily — every call to
tempdir()first sweeps anything already due. - On demand — run
ephemdir sweepfrom the command line.
To clean up reliably over time (and right after a reboot), install a scheduled sweep service for your platform — one command, no manual unit files:
ephemdir install-service --interval 600 # launchd / systemd / Task Scheduler
ephemdir uninstall-service
Prefer to wire it up yourself? The equivalents are:
- Linux (cron):
*/10 * * * * ephemdir sweep - macOS (launchd): a
LaunchAgentrunningephemdir sweep; template inpackaging/. - Windows (Task Scheduler): a task running
ephemdir sweepat logon and on a repeating interval.
You can also keep a foreground watcher running:
ephemdir watch --interval 600
Configuration
Set per-user defaults in a config.toml file in your config directory
(~/.config/ephemdir/ on Linux, ~/Library/Application Support/ephemdir/ on
macOS, %APPDATA%\ephemdir\ on Windows). Every key is optional:
lifetime = "6h"
remove_on_restart = true
keep_while_in_use = true
prefix = "scratch-"
words = 2
These apply to any option you do not pass explicitly to tempdir() or the
ephemdir new command. Reading the file uses the standard-library tomllib
(Python 3.11+) or tomli on older versions.
Command-line interface
ephemdir new # create a directory, print its path
ephemdir new --lifetime 2h # with a lifetime
ephemdir new --keep-on-restart # do not remove on restart
ephemdir list # show tracked directories
ephemdir sweep # remove everything due now
ephemdir sweep --force # remove every tracked directory
ephemdir watch # sweep periodically in the foreground
ephemdir install-service # schedule sweeps via the OS scheduler
ephemdir uninstall-service # remove the scheduled service
Add -v for more output or -q to stay quiet. The new command prints the
path to stdout and all diagnostics to stderr, so it composes cleanly:
cd "$(ephemdir new)"
Development
pip install -e ".[dev]"
pytest
License
MIT © vindfjur
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ephemdir-0.2.0.tar.gz.
File metadata
- Download URL: ephemdir-0.2.0.tar.gz
- Upload date:
- Size: 25.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f9246871bc72abadc5882339de4794e8ac67379fda498939f32bfd7e3f49342
|
|
| MD5 |
40641305cfe3d1df6ace3c800d3daea5
|
|
| BLAKE2b-256 |
fc62ac45b5ea40dcfd62734929e7f03753179f44d248bb1ac79b9ebd42bdaeae
|
Provenance
The following attestation bundles were made for ephemdir-0.2.0.tar.gz:
Publisher:
publish.yml on vindfjur/ephemdir
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ephemdir-0.2.0.tar.gz -
Subject digest:
2f9246871bc72abadc5882339de4794e8ac67379fda498939f32bfd7e3f49342 - Sigstore transparency entry: 1735546096
- Sigstore integration time:
-
Permalink:
vindfjur/ephemdir@5366f31ec1a359a9397814b8c2eddac271ad5f2c -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/vindfjur
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5366f31ec1a359a9397814b8c2eddac271ad5f2c -
Trigger Event:
release
-
Statement type:
File details
Details for the file ephemdir-0.2.0-py3-none-any.whl.
File metadata
- Download URL: ephemdir-0.2.0-py3-none-any.whl
- Upload date:
- Size: 22.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fe3e637118b810d9b3d13be27f599621603d438ed5369dd912383cdfd5fcfb7
|
|
| MD5 |
027bc85ecf4009a189c0bd914d52d9fc
|
|
| BLAKE2b-256 |
b92245e3835cdc13e21269687e2ae8847e2db4b2cb7dffd17725275c632da68f
|
Provenance
The following attestation bundles were made for ephemdir-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on vindfjur/ephemdir
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ephemdir-0.2.0-py3-none-any.whl -
Subject digest:
9fe3e637118b810d9b3d13be27f599621603d438ed5369dd912383cdfd5fcfb7 - Sigstore transparency entry: 1735546131
- Sigstore integration time:
-
Permalink:
vindfjur/ephemdir@5366f31ec1a359a9397814b8c2eddac271ad5f2c -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/vindfjur
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5366f31ec1a359a9397814b8c2eddac271ad5f2c -
Trigger Event:
release
-
Statement type: