← All ivy-nodes
IVYXSTUDIO · IVY NODE
F
File Write
ivy.node.file-write · v0.1.0
ivyx✓
Writes text to a file, creating its folder, and returns the absolute path and the number of bytes written. Refuses to replace an existing file unless overwrite is true.
#file#write#report#save
Inputs
| Field | Type | Description |
|---|---|---|
| pathrequired | string | Where to write. A relative path is relative to the workspace. |
| textrequired | string | The text to write, as UTF-8. |
| overwrite | boolean | Replace the file when it already exists. |
Outputs
| Field | Type | Description |
|---|---|---|
| pathrequired | string | The absolute path written. |
| bytesrequired | integer | How many bytes were written. |
Source
python
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
import os
path = inp["path"]
text = inp["text"]
overwrite = bool(inp.get("overwrite", False))
if os.path.exists(path) and not overwrite:
raise FileExistsError(f"{path} exists; pass overwrite: true to replace it.")
os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
data = text.encode("utf-8")
with open(path, "wb") as handle:
handle.write(data)
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["path"] = os.path.abspath(path)
out["bytes"] = len(data)Tests
Requires: python:3.9
- writes
A new file in a new folder holds the text, in UTF-8 bytes.
- no-overwrite
An existing file is not replaced by default.