Stdlib-only filesystem primitives — atomic write, XDG trash, safe walk, mount helpers.
Project description
┌──────────────────────────────────────────────────┐
│ c o d e c h u — f s │
│ . │
│ ├── atomic_write() safe.tmp -> fsync -> mv │
│ ├── move_to_trash() ~/.local/share/Trash/ │
│ ├── walk() cancellable + loop-safe │
│ └── mountpoint() /proc/mounts aware │
└──────────────────────────────────────────────────┘
Filesystem primitives for tools that must not lose data.
codechu-fs
Stdlib-only filesystem helpers — atomic writes, XDG trash, cancellable walks, mount discovery — extracted from the Disk Cleaner toolchain. Zero third-party dependencies. Python 3.10+.
What it gives you
atomic_write— tempfile in the same dir,fsync, thenrename. Preserves existing permissions. Either bytes or text (with encoding).move_to_trash— freedesktop XDG Trash Specification compliant. HonorsXDG_DATA_HOME, writes a.trashinfo, resolves name collisions.walk—os.walkwith three thingsos.walkis missing: acancelEvent for cooperative shutdown, anon_errorcallback (no silent swallow), and symlink-loop detection when following links.recursive_size— sum of bytes under a directory, cancellable.mountpoint— the mount that contains a path, parsed from/proc/mountson Linux with ast_devfallback elsewhere.is_safe_path— path-traversal guard for user-supplied inputs.temp_dir— context-managed temp directory, auto-removed.
Install
pip install codechu-fs
Quick examples
from pathlib import Path
from threading import Event
from codechu_fs import (
atomic_write, move_to_trash, walk, recursive_size,
mountpoint, is_safe_path, temp_dir,
)
# Durable write — never leaves a half-written file even if you crash mid-write.
atomic_write("/etc/myapp/config.json", b'{"k":1}\n')
atomic_write("notes.txt", "hello\n", encoding="utf-8")
# Send a file to the desktop trash (recoverable from the file manager).
trashed = move_to_trash("/tmp/junk.log")
print("now at:", trashed)
# Cancellable walk — stop instantly when the user clicks Cancel.
cancel = Event()
for dirpath, dirnames, filenames in walk("/var/log", cancel=cancel):
if some_condition():
cancel.set()
# Total size of a directory tree (skips unreadable files).
print(recursive_size(Path.home() / "Downloads"))
# What partition is this on?
print(mountpoint("/var/log/syslog")) # → /var, or / on most systems
# Safe extraction of an untrusted filename.
if is_safe_path(user_input, base="/srv/uploads"):
...
# Scratch space that always cleans up.
with temp_dir(prefix="myapp-") as scratch:
(scratch / "work.bin").write_bytes(b"...")
Design
- Pure stdlib. Zero third-party dependencies. Seven small modules.
- Defensive.
walknever swallows errors silently.atomic_writefsyncs before rename.move_to_trashrolls back its.trashinfoon failure. - Cancellable. Every traversal accepts a
threading.Event. Long scans on slow disks remain responsive in GUI apps.
Tests
pip install -e ".[dev]"
pytest -q
Documentation
- API reference — every public symbol, signatures, edge cases
- Recipes — five idiomatic patterns for CLIs and GUIs
License
MIT — see LICENSE.
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 codechu_fs-0.1.0.tar.gz.
File metadata
- Download URL: codechu_fs-0.1.0.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38dec0a05582e72e4f0b6761e759b475e0ec514e64b1225ae11f875c2ef70d9b
|
|
| MD5 |
9db94a754c5e519fa057d054b6add825
|
|
| BLAKE2b-256 |
710ff6c783bcb929bf0cac486134cb49394fa0db611d3dc85f93bc675100034c
|
Provenance
The following attestation bundles were made for codechu_fs-0.1.0.tar.gz:
Publisher:
release.yml on codechu/fs-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codechu_fs-0.1.0.tar.gz -
Subject digest:
38dec0a05582e72e4f0b6761e759b475e0ec514e64b1225ae11f875c2ef70d9b - Sigstore transparency entry: 1582277931
- Sigstore integration time:
-
Permalink:
codechu/fs-py@45bf0d1f3774bd493c28cf5d3e2057831cd18f0e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/codechu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@45bf0d1f3774bd493c28cf5d3e2057831cd18f0e -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file codechu_fs-0.1.0-py3-none-any.whl.
File metadata
- Download URL: codechu_fs-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.4 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 |
c214540cd87295766bcc6b3c4143845f376fec1c970a885d4812b0d12851af2e
|
|
| MD5 |
53e72c089f58084d82c9ee3b59660242
|
|
| BLAKE2b-256 |
9f7c0dd48f493d0df56c8f56cba63e310f0c1b8b28021a7a00841fc87ef8007f
|
Provenance
The following attestation bundles were made for codechu_fs-0.1.0-py3-none-any.whl:
Publisher:
release.yml on codechu/fs-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
codechu_fs-0.1.0-py3-none-any.whl -
Subject digest:
c214540cd87295766bcc6b3c4143845f376fec1c970a885d4812b0d12851af2e - Sigstore transparency entry: 1582278048
- Sigstore integration time:
-
Permalink:
codechu/fs-py@45bf0d1f3774bd493c28cf5d3e2057831cd18f0e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/codechu
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@45bf0d1f3774bd493c28cf5d3e2057831cd18f0e -
Trigger Event:
workflow_dispatch
-
Statement type: