Safe
provide.foundation.file.safe
¶
TODO: Add module docstring.
Functions¶
safe_copy
¶
safe_copy(
src: Path | str,
dst: Path | str,
overwrite: bool = False,
preserve_mode: bool = True,
) -> None
Copy file safely with metadata preservation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
Path | str
|
Source file path |
required |
dst
|
Path | str
|
Destination file path |
required |
overwrite
|
bool
|
Whether to overwrite existing destination |
False
|
preserve_mode
|
bool
|
Whether to preserve file permissions |
True
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If source doesn't exist |
FileExistsError
|
If destination exists and overwrite=False |
OSError
|
If copy operation fails |
Source code in provide/foundation/file/safe.py
safe_delete
¶
Delete file safely.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
File to delete |
required |
missing_ok
|
bool
|
If True, don't raise error if file doesn't exist |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if deleted, False if didn't exist |
Raises:
| Type | Description |
|---|---|
OSError
|
If deletion fails and file exists |
Source code in provide/foundation/file/safe.py
safe_move
¶
Move file safely with optional overwrite.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
Path | str
|
Source file path |
required |
dst
|
Path | str
|
Destination file path |
required |
overwrite
|
bool
|
Whether to overwrite existing destination |
False
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If source doesn't exist |
FileExistsError
|
If destination exists and overwrite=False |
OSError
|
If move operation fails |
Source code in provide/foundation/file/safe.py
safe_read
¶
safe_read(
path: Path | str,
default: bytes | None = None,
encoding: str | None = None,
) -> bytes | str | None
Read file safely, returning default if not found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
File to read |
required |
default
|
bytes | None
|
Value to return if file doesn't exist |
None
|
encoding
|
str | None
|
If provided, decode bytes to str |
None
|
Returns:
| Type | Description |
|---|---|
bytes | str | None
|
File contents or default value |
Source code in provide/foundation/file/safe.py
safe_read_text
¶
Read text file safely with default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
File to read |
required |
default
|
str
|
Default text if file doesn't exist |
''
|
encoding
|
str
|
Text encoding |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
str
|
File contents or default text |