File primitives for Python
Project description
file-primitives
Python file primitives
read_file: reads file with an option to specify default if file does not exist or a custom reader (likereader = lambda file: json.read(file))write_file: writes file with auto-directory creation, custom writer, and optional atomic writesensure_dir: ensures a directory existsdelete_path: deletes a file or directory. Returnsboolindicating success.
def read_file(
path: Union[str, Path],
bytes: bool = False,
reader: Callable[..., T] = lambda file: file.read(),
default: Union[D, object] = MISSING, # if file does not exist
encoding: str = "utf-8",
) -> Union[T, D]:
pass
def write_file(
path: Union[str, Path],
data: T,
bytes: bool = False,
writer: Callable[[T, Any], Any] = lambda data, file: file.write(data),
encoding: str = "utf-8",
ensure_dir: bool = True,
atomic: bool = False,
) -> None:
pass
def ensure_dir(
path: T, # T = TypeVar("T", str, Path)
is_file: bool = False,
) -> T:
pass
def delete_path(
path: Union[str, Path],
missing_ok: bool = False,
delete_empty_parents: bool = False,
) -> bool:
pass
...
from file_primitives import read_file, write_file, ensure_dir, delete_path
# Simple file operations
content = read_file("config.json")
# Binary mode
binary_content = read_file("image.png", bytes=True)
# output dir will be created automatically
write_file("output/data.json", {"key": "value"}, writer=lambda data, file: json.dump(data, file))
# Atomic write (prevents corruption)
write_file("important.json", data, atomic=True)
# With error handling
config = read_file("missing.json", default="I am missing")
ensure_dir("logs/2024/january") # Creates full path
ensure_dir("reports/summary.pdf", is_file=True) # Creates "reports/" directory
# Delete operations
deleted = delete_path("temp/file.txt") # Returns True if deleted, False if not found (with missing_ok=True)
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
file_primitives-0.2.0.tar.gz
(42.1 kB
view details)
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 file_primitives-0.2.0.tar.gz.
File metadata
- Download URL: file_primitives-0.2.0.tar.gz
- Upload date:
- Size: 42.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1c8745a8bc2d8f4a44c061a0eb0c52b62891f752f6639bbbb9cb23628af4565e
|
|
| MD5 |
2bcf0ee88aade67f54ab0d142df260c5
|
|
| BLAKE2b-256 |
3873165596732fff22ed9524aa4ba0e49cafca5bb1816d3b943d1557fee4c3c7
|
File details
Details for the file file_primitives-0.2.0-py3-none-any.whl.
File metadata
- Download URL: file_primitives-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61758f5e22ed37002ecaaa75f81f170e9c688de207c6f1d5e103b757a2b13f72
|
|
| MD5 |
8cc2cfb263e44c5bf74d77483dff1a6a
|
|
| BLAKE2b-256 |
95b92a5a62f58b7b8e42571412b47c3381e01ea3939a68439349e3b9890fb27b
|