File primitives for Python
Project description
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, _MissingType] = MISSING,
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 by default
write_file("output/data.json", {"key": "value"}, writer=lambda data, file: json.dump(data, file))
# Atomic write
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
delete_path("temp/file.txt")
deleted = delete_path("temp/file.txt", 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.1.tar.gz
(41.8 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.1.tar.gz.
File metadata
- Download URL: file_primitives-0.2.1.tar.gz
- Upload date:
- Size: 41.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f70e58dfbbbe2618d4645f1aaf6d5e7d49f68136d86486312b0a88275bbcde6
|
|
| MD5 |
0f4e293cd14c64eaf65b1104741e1812
|
|
| BLAKE2b-256 |
250cb3cfd5cc741811bf9a22a5687cc0e1d0bf8eaff1f6d1ff378bd7d68dfb68
|
File details
Details for the file file_primitives-0.2.1-py3-none-any.whl.
File metadata
- Download URL: file_primitives-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c8e52eac6d1c5d81c620f733582ac4ddc344230d35aef9eba590c5c1cf4544e
|
|
| MD5 |
5c630f8d9559be063019dab096c44c79
|
|
| BLAKE2b-256 |
089a287e10aa0f5443e5ce58a6bd67b90756aa2db36181a581299379e2446021
|