Cross-platform file system utilities for Python
Project description
ZeroFileSystem
Cross-platform file system utilities for Python 3.12+
Why This Project Exists
Working with files in Python often requires combining multiple standard library modules (os, shutil, pathlib, json, gzip, tarfile, zipfile) with careful attention to platform differences, atomic operations, and error handling. zerofilesystem consolidates these operations into a single, cohesive library that handles the complexity for you.
The library solves common pain points: ensuring atomic writes don't corrupt files on crash, managing cross-platform file locking, handling path normalization across Windows and Unix, and providing transactional file operations with automatic rollback. Whether you're building a configuration manager, a backup utility, or any application that needs reliable file operations, zerofilesystem provides battle-tested primitives that work consistently across platforms.
Technical Choices and Design
zerofilesystem uses only the Python standard library with no external dependencies. This deliberate choice ensures maximum compatibility, minimal installation footprint, and no dependency conflicts.
The library is organized around single-responsibility classes, each handling one category of file operations. All public functions accept both str and pathlib.Path objects via the Pathish type alias. Atomic operations use the temp-file-plus-rename pattern to ensure crash safety on POSIX-compliant filesystems.
Platform-specific behavior (file locking via fcntl/msvcrt, hidden file attributes, executable permissions) is abstracted behind unified APIs that do the right thing on each platform.
High-Level Component Overview
The library is structured into functional modules:
- Basic I/O (
FileIO,JsonHandler,GzipHandler) - Text and binary file reading/writing with atomic support, JSON serialization, and gzip compression - File Discovery (
FileFinder,Finder) - Glob-based file search with custom filters and lazy iteration;Finderprovides a fluent builder API - File Operations (
FileSync,FileHasher,FileMeta,FileUtils) - Conditional copy/move, cryptographic hashing, metadata access, and filename sanitization - Path Utilities (
PathUtils) - Cross-platform path normalization, expansion, and validation - Permissions (
FilePermissions) - Read/write/execute attributes and timestamp manipulation - Directory Operations (
DirectoryOps) - Tree copy, move, sync, and temporary directories - Integrity (
IntegrityChecker) - Directory hashing, manifest creation, and verification - Transactions (
FileTransaction) - Multi-file atomic operations with rollback - Security (
SecureOps) - Secure file deletion and private directory creation - Archives (
ArchiveHandler) - Tar and zip creation/extraction with filtering - File Watching (
FileWatcher,Watcher) - Polling-based filesystem monitoring;Watcherprovides a fluent builder API with debouncing - Locking (
FileLock) - Cross-platform advisory file locking
Meaningful Usage Example
Here's a realistic example showing how zerofilesystem components work together to create a simple backup system with integrity verification:
import zerofilesystem as zfs
# Create a backup of a project directory
source_dir = "./my_project"
backup_dir = "./backups"
# Create timestamped backup archive
zfs.ensure_dir(backup_dir)
archive_path = zfs.create_zip(
source_dir,
f"{backup_dir}/backup_2024.zip",
filter_fn=lambda p: not p.name.startswith("."), # Exclude hidden files
compression="deflated"
)
# Generate integrity manifest for verification
manifest = zfs.create_manifest(source_dir, algorithm="sha256")
zfs.save_manifest(manifest, f"{backup_dir}/backup_2024.manifest")
# Later: verify the backup matches the manifest
loaded_manifest, algo = zfs.load_manifest(f"{backup_dir}/backup_2024.manifest")
result = zfs.verify_manifest(source_dir, loaded_manifest, algorithm=algo)
if result.is_valid:
print("Backup integrity verified!")
else:
print(f"Modified: {result.modified}, Missing: {result.missing}")
# Use transactions for multi-file config updates
with zfs.FileTransaction() as tx:
tx.write_text("config/app.json", '{"version": 2}')
tx.write_text("config/db.json", '{"host": "localhost"}')
# Both files written atomically, or neither if an error occurs
Installation
pip install zerofilesystem
Or install from source:
git clone https://github.com/francescofavi/zerofilesystem.git
cd zerofilesystem
pip install -e .
Requirements
- Python 3.12+
- No external dependencies - uses only the Python standard library
Main Functions and APIs
File I/O
read_text(path, encoding="utf-8") - Read text file contents.
import zerofilesystem as zfs
# Simple
content = zfs.read_text("config.txt")
# With encoding
content = zfs.read_text("data.txt", encoding="latin-1")
write_text(path, data, atomic=True, create_dirs=True) - Write text with atomic safety.
import zerofilesystem as zfs
# Simple
zfs.write_text("output.txt", "Hello World")
# Non-atomic write to existing directory
zfs.write_text("logs/app.log", log_data, atomic=False, create_dirs=False)
read_json(path) / write_json(path, obj) - JSON file operations.
import zerofilesystem as zfs
# Simple
config = zfs.read_json("settings.json")
# Write with custom formatting
zfs.write_json("data.json", {"users": users}, indent=4, atomic=True)
File Discovery
find_files(base_dir, pattern, filter_fn, recursive, max_results) - Find files matching criteria.
import zerofilesystem as zfs
# Simple - find all Python files
py_files = zfs.find_files("./src", pattern="*.py")
# Complex - find large log files, limit results
large_logs = zfs.find_files(
"./logs",
pattern="*.log",
filter_fn=lambda p: p.stat().st_size > 1024 * 1024,
recursive=True,
max_results=100
)
walk_files(base_dir, pattern) - Memory-efficient generator for large directories.
import zerofilesystem as zfs
# Process millions of files without loading all paths
for path in zfs.walk_files("/data", pattern="*.csv"):
process_file(path)
Finder (Fluent API)
Finder(base_dir) - Powerful file finder with fluent builder API.
from zerofilesystem import Finder
# Find Python files modified in last 7 days
files = (Finder("./src")
.patterns("*.py")
.modified_last_days(7)
.not_hidden()
.find())
# Complex search with multiple filters
files = (Finder("./project")
.patterns("*.py", "*.json")
.exclude("__pycache__", ".git", "*.pyc")
.size_min("1KB")
.size_max("10MB")
.not_empty()
.max_depth(5)
.limit(100)
.find())
# Memory-efficient iteration
for path in Finder("./logs").patterns("*.log").walk():
process_log(path)
# Quick checks
count = Finder("./src").patterns("*.py").count()
has_tests = Finder("./tests").patterns("test_*.py").exists()
Watcher (Fluent API)
Watcher(base_dir) - File system watcher with fluent builder API and debouncing.
from zerofilesystem import Watcher, EventType
# Simple watch
watcher = (Watcher("./src")
.patterns("*.py")
.on_any(lambda e: print(f"{e.type.name}: {e.path}"))
.start())
# Watch with filtering and debouncing
watcher = (Watcher("./project")
.patterns("*.py", "*.json")
.exclude("__pycache__", ".git")
.not_hidden()
.poll_fast() # 0.1 second polling
.debounce(0.5) # Wait 500ms after last change
.on_created(handle_new)
.on_modified(handle_change)
.on_deleted(handle_remove)
.start())
# Context manager
with Watcher("./config").patterns("*.yaml").on_any(reload) as w:
run_server() # Watcher stops automatically on exit
File Locking
FileLock(path, timeout) - Cross-platform advisory file lock.
import zerofilesystem as zfs
# Simple - blocking lock
with zfs.FileLock("/tmp/myapp.lock"):
do_critical_work()
# With timeout
try:
with zfs.FileLock("/tmp/myapp.lock", timeout=5.0):
do_critical_work()
except TimeoutError:
print("Could not acquire lock")
Transactions
FileTransaction() - Atomic multi-file operations with rollback.
import zerofilesystem as zfs
# Simple
with zfs.FileTransaction() as tx:
tx.write_text("a.txt", "content a")
tx.write_text("b.txt", "content b")
# With explicit control
tx = zfs.FileTransaction()
try:
tx.write_text("config.json", new_config)
tx.copy_file("template.txt", "output.txt")
tx.commit()
except Exception:
tx.rollback()
Archives
create_zip(source, output) / create_tar(source, output) - Create archives.
import zerofilesystem as zfs
# Simple
zfs.create_zip("./project", "backup.zip")
# With compression and filtering
zfs.create_tar(
"./data",
"archive.tar.gz",
compression="gz",
filter_fn=lambda p: p.suffix != ".tmp"
)
extract(archive, destination) - Auto-detect and extract archives.
import zerofilesystem as zfs
# Auto-detects format
zfs.extract("backup.zip", "./restored")
zfs.extract("archive.tar.gz", "./restored")
Integrity Checking
file_hash(path, algo) - Compute file hash.
import zerofilesystem as zfs
# Simple
sha = zfs.file_hash("document.pdf")
# With progress callback for large files
def progress(done, total):
print(f"\r{done}/{total} bytes", end="")
sha = zfs.file_hash("large.iso", algo="sha256", progress_callback=progress)
directory_hash(path) - Hash entire directory tree.
import zerofilesystem as zfs
# Detect any changes in a directory
before = zfs.directory_hash("./config")
# ... operations ...
after = zfs.directory_hash("./config")
if before != after:
print("Configuration changed!")
Directory Operations
copy_tree(src, dst) / move_tree(src, dst) - Recursive directory operations.
import zerofilesystem as zfs
# Simple copy
result = zfs.copy_tree("./src", "./backup")
print(f"Copied {len(result.copied)} files")
# Sync with conflict handling
result = zfs.copy_tree(
"./new_version",
"./deploy",
on_conflict="only_if_newer",
preserve_metadata=True
)
sync_dirs(src, dst, delete_extra) - Mirror directories.
import zerofilesystem as zfs
# One-way sync, optionally delete extra files in destination
result = zfs.sync_dirs("./source", "./mirror", delete_extra=True)
Secure Operations
secure_delete(path, passes) - Overwrite before deletion.
import zerofilesystem as zfs
# Simple
zfs.secure_delete("sensitive.txt")
# Multiple passes with random data
zfs.secure_delete("credentials.json", passes=7, random_data=True)
Development
This project uses uv for dependency management.
# Install dependencies
uv sync
# Run tests
uv run pytest
# Linting and formatting
uv run ruff check .
uv run ruff format .
# Type checking
uv run mypy src
Further Documentation
- Full API Reference - Detailed documentation of all public functions, parameters, and return types
- Architecture - Internal structure, module organization, dependency graph, and design decisions
- Functional Analysis - High-level view of processes, flows, and feature mapping
License
MIT License Copyright (c) 2025 Francesco Favi
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
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 zerofilesystem-0.1.3.tar.gz.
File metadata
- Download URL: zerofilesystem-0.1.3.tar.gz
- Upload date:
- Size: 82.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba16bcc4708c1386ed2eec98bcf4cdaf9193f0d3d2d815e87b90f370983cef72
|
|
| MD5 |
6e369407d11376180a8653eb95c88fd2
|
|
| BLAKE2b-256 |
e78173024db472ec88431dbd302a268d14a047b01ca030df8dfd334cc9398f69
|
Provenance
The following attestation bundles were made for zerofilesystem-0.1.3.tar.gz:
Publisher:
publish.yml on francescofavi/zerofilesystem
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zerofilesystem-0.1.3.tar.gz -
Subject digest:
ba16bcc4708c1386ed2eec98bcf4cdaf9193f0d3d2d815e87b90f370983cef72 - Sigstore transparency entry: 1203706654
- Sigstore integration time:
-
Permalink:
francescofavi/zerofilesystem@228b3ad08b18ac07d998d71f6b072d0dbfa98646 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/francescofavi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@228b3ad08b18ac07d998d71f6b072d0dbfa98646 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file zerofilesystem-0.1.3-py3-none-any.whl.
File metadata
- Download URL: zerofilesystem-0.1.3-py3-none-any.whl
- Upload date:
- Size: 52.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92123daaebb316b67d082e5aa01c5c964bca421c6815b2d9a09f2c09993ca458
|
|
| MD5 |
4f6cdcc80127b39cb2235eaed6f9fb02
|
|
| BLAKE2b-256 |
53bbfb4a23d1ef83157afe01f4d7cbf1aabbd92b117f397570c7a70890f53dd6
|
Provenance
The following attestation bundles were made for zerofilesystem-0.1.3-py3-none-any.whl:
Publisher:
publish.yml on francescofavi/zerofilesystem
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
zerofilesystem-0.1.3-py3-none-any.whl -
Subject digest:
92123daaebb316b67d082e5aa01c5c964bca421c6815b2d9a09f2c09993ca458 - Sigstore transparency entry: 1203706672
- Sigstore integration time:
-
Permalink:
francescofavi/zerofilesystem@228b3ad08b18ac07d998d71f6b072d0dbfa98646 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/francescofavi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@228b3ad08b18ac07d998d71f6b072d0dbfa98646 -
Trigger Event:
workflow_dispatch
-
Statement type: