Fichîes Locales

write()

files.write(file, content) → Promise<Item>

Scrie un file e restitue ciò che è scritto. Texto o binario, i genitori creati, e un file existente è sostituito.

freereplacestext and binary
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

Parâmetros

filestringobrigatório

Il sentiero da scriver, parent al workspace.

contentstring | Buffer | Uint8Array | ArrayBufferobrigatório

Quaè scriver.Qualque altra cosa è renied.

Texto

const item = await pc.files.write("Launch plan/brief.md", "# Brief\n\nShip in March.");
// { name: "brief.md", path: "Launch plan/brief.md", type: "file", size: 29,
//   mimeType: "text/markdown", modifiedAt: "…" }
item = pc.files.write("Launch plan/brief.md", "# Brief\n\nShip in March.")
# {"path": "Launch plan/brief.md", "size": 29}

Una stringa è scritta come UTF-8. Il mimeType che torna è lettura da l'estensione, non dal contenuto — quindi il nome che scegli è lo che decide come il file è trattato successivamente.

Binario

// A generated image, straight from a model session
const [picture] = await session.generate({
  inputs: [{ type: "text", content: "A fox in the snow" }],
  outputs: [{ type: "image" }],
}).then((r) => r.outputs);

await pc.files.write("Assets/fox.png", Buffer.from(picture.content, "base64"));

// A download
const response = await fetch("https://example.com/report.pdf");
await pc.files.write("Reports/report.pdf", await response.arrayBuffer());
import base64, urllib.request

# A generated image, straight from a model session
picture = session.generate(
    inputs=[{"type": "text", "content": "A fox in the snow"}],
    outputs=[{"type": "image"}],
)["outputs"][0]

pc.files.write("Assets/fox.png", base64.b64decode(picture["content"]))

# A download
with urllib.request.urlopen("https://example.com/report.pdf") as response:
    pc.files.write("Reports/report.pdf", response.read())

Soplat e non conserva nulla

Non vi è una versiòn anteriore qui

scrit() è l'operazione semplice: il vecchio contenuto è partito. Mantenendo una copia è ciò che i propri strumenti di scriptura del agente aggiungono in cima, sotto .versions/ — se si desidera che comportament in vostro codice, legga il file prima e scrivi la copia tu stesso.

// Keep a copy before replacing
const before = await pc.files.read("notes.md");
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
await pc.files.write(`.versions/notes.md/${stamp}.md`, before.content);
await pc.files.write("notes.md", next);
from datetime import datetime, timezone

# Keep a copy before replacing
before = pc.files.read("notes.md")
stamp = datetime.now(timezone.utc).isoformat().replace(":", "-").replace(".", "-")
pc.files.write(f".versions/notes.md/{stamp}.md", before["content"])
pc.files.write("notes.md", next_content)

I dossier sono creatis se necessari

Scrivere Clients/Acme/2026/invoice.md crea i tre folders in via. Un appello, a prescindere da profonda.

Erros

CódigoStatusQuando
missing_content400content is undefined or null. Una string vazio è valida.
invalid_content400Ni una corda, un Buffer, un Uint8Array o un ArrayBuffer.
invalid_path400Absolute, fuori del luogo de lavoro, o la raiz del luogo de lavoro.
local_files_require_node400O ambiente de execução não tem sistema de arquivos.
storage_not_on_this_machine409Un altro computer's stoccage.

Un oggetto è no serializati per te: passa JSON.stringify(value), quindi la formattura sta tua.