Save text file
Write text files while creating parent directories and enforcing overwrite policy.
save_text_file(*, text: str, directory: Path, filename: str, overwrite: bool = False, encoding: str = 'utf-8', add_newline: bool = True) -> Path
¶
Write text to a file in the target output directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text content to write. |
required |
directory
|
Path
|
Directory that should contain the output file. |
required |
filename
|
str
|
Name of the output file to create. |
required |
overwrite
|
bool
|
If true, replace an existing file. If false, raise an error when the target file already exists. |
False
|
encoding
|
str
|
Text encoding to use when writing the file. |
'utf-8'
|
add_newline
|
bool
|
If true, append a newline character to the end of the text after writing. |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path object for the written file. |
Raises:
| Type | Description |
|---|---|
FileExistsError
|
If the target file exists and overwrite is false. |
OSError
|
If the parent directory cannot be created or the file cannot be written. |
Notes
Parent directories are created automatically when missing.