Skip to main content

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, then rename. Preserves existing permissions. Either bytes or text (with encoding).
  • move_to_trash — freedesktop XDG Trash Specification compliant. Honors XDG_DATA_HOME, writes a .trashinfo, resolves name collisions.
  • walkos.walk with three things os.walk is missing: a cancel Event for cooperative shutdown, an on_error callback (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/mounts on Linux with a st_dev fallback 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. walk never swallows errors silently. atomic_write fsyncs before rename. move_to_trash rolls back its .trashinfo on 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


Download files

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

Source Distribution

codechu_fs-0.1.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

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

codechu_fs-0.1.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

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

Hashes for codechu_fs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 38dec0a05582e72e4f0b6761e759b475e0ec514e64b1225ae11f875c2ef70d9b
MD5 9db94a754c5e519fa057d054b6add825
BLAKE2b-256 710ff6c783bcb929bf0cac486134cb49394fa0db611d3dc85f93bc675100034c

See more details on using hashes here.

Provenance

The following attestation bundles were made for codechu_fs-0.1.0.tar.gz:

Publisher: release.yml on codechu/fs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for codechu_fs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c214540cd87295766bcc6b3c4143845f376fec1c970a885d4812b0d12851af2e
MD5 53e72c089f58084d82c9ee3b59660242
BLAKE2b-256 9f7c0dd48f493d0df56c8f56cba63e310f0c1b8b28021a7a00841fc87ef8007f

See more details on using hashes here.

Provenance

The following attestation bundles were made for codechu_fs-0.1.0-py3-none-any.whl:

Publisher: release.yml on codechu/fs-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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