ddp_utils.temp.temp_file¶
import ddp_utils.temp.temp_file
Wrap a temporary file path with I/O helpers and optional cleanup.
TempFile reads and writes text or bytes and can remove its file when a
managed context exits.
Examples
Use this public operation:
from pathlib import Path
from ddp_utils.temp.temp_file import TempFile
temporary = TempFile(Path("work.tmp"), auto_cleanup=True)
- class ddp_utils.temp.temp_file.TempFile(path: Path, auto_cleanup: bool = True)¶
Bases:
objectWrapper around a temporary file path.
This class provides convenience methods for writing and reading text or bytes, and supports automatic cleanup in a context manager.
Parameters
Name
Type
Description
path
Path
Filesystem path of the temporary file.
auto_cleanup
bool
If
True, the file is removed when exiting the context manager.Examples
Use this public operation:
instance = TempFile(...)
Bind file helpers to a path without creating the file.
Parameters
Name
Type
Description
path
Path
File represented by this wrapper.
auto_cleanup
bool
Remove the file when a managed context exits.
Examples
Configure a wrapper that preserves its file after context exit:
temporary = TempFile(Path("result.txt"), auto_cleanup=False)
- property path: Path¶
Return the underlying file path.
Returns
File path.
Examples
Use this public operation:
result = instance.path()
- exists() bool¶
Check whether the file exists.
Returns
Type
Description
bool
Trueif the file exists, elseFalse.Examples
Use this public operation:
result = instance.exists()
- write_text(data: str, encoding: str = 'utf-8') Path¶
Write text data to the file.
Parameters
Name
Type
Description
data
str
Text content to write.
encoding
str
Text encoding.
Returns
Type
Description
Path
The file path.
Examples
Use this public operation:
result = instance.write_text(data=data_value)
- write_bytes(data: bytes) Path¶
Write binary data to the file.
Parameters
Name
Type
Description
data
bytes
Binary content to write.
Returns
Type
Description
Path
The file path.
Examples
Use this public operation:
result = instance.write_bytes(data=data_value)
- read_text(encoding: str = 'utf-8') str¶
Read text content from the file.
Parameters
Name
Type
Description
encoding
str
Text encoding.
Returns
Type
Description
str
File content as text.
Examples
Use this public operation:
result = instance.read_text()
- read_bytes() bytes¶
Read binary content from the file.
Returns
Type
Description
bytes
File content as bytes.
Examples
Use this public operation:
result = instance.read_bytes()
- cleanup() None¶
Remove the file if it exists.
Examples
Use this public operation:
result = instance.cleanup()