Saltar al contenido

Sistema de Archivos (std::fs)

En esta página

std::fs toca el disco: lectura, escritura, eliminación, listado de directorios y rutas temporales. Como todas las operaciones interactúan con el sistema de archivos del host, no se pueden ejecutar en el sandbox WASM del navegador; ejecútalos con la CLI de Zolo.


Leer y escribir archivos

fs.write(path, content) escribe texto; fs.read(path) carga el contenido de vuelta como string; fs.remove(path) borra el archivo. Los ejemplos siguientes usan fs.temp_dir() para no contaminar el repositorio:

Crea, lee y elimina un archivo de texto en el directorio temporal del sistema.

01-read-write.zolo
Playground
// Feature: fs.write / fs.read — simple round-trip

// When to use: persist/load text on disk. Note: we use fs.temp_dir()

// to write to the system temp area and avoid littering the repo.


use std::fs

let dir = fs.temp_dir()
let path = dir + "/zolo-fs-roundtrip.txt"

let content = "hello, zolo!\nline 2"
fs.write(path, content)

// Read it back.

let loaded = fs.read(path)
print(loaded == content)  // expected: true

print(loaded.contains("zolo"))  // expected: true


// Cleanup.

fs.remove(path)
print(fs.exists(path))  // expected: false

Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.


Verificar existencia y tipo

fs.exists distingue rutas existentes de inexistentes. fs.is_file y fs.is_dir diferencian archivos regulares de directorios:

Demuestra exists, is_file e is_dir antes y después de crear un archivo.

02-exists-and-stat.zolo
Playground
// Feature: fs.exists / fs.is_file / fs.is_dir — existence checks

// When to use: verify before reading/writing, distinguish kind.


use std::fs

let dir = fs.temp_dir()
let path = dir + "/zolo-fs-exists.txt"

// Before creating — does not exist.

print(fs.exists(path))  // expected: false


fs.write(path, "hello")
print(fs.exists(path))  // expected: true

print(fs.is_file(path))  // expected: true

print(fs.is_dir(path))  // expected: false


// temp_dir itself is a directory.

print(fs.exists(dir))  // expected: true

print(fs.is_dir(dir))  // expected: true

print(fs.is_file(dir))  // expected: false


// Cleanup.

fs.remove(path)
print(fs.exists(path))  // expected: false

Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.


Listar directorios

fs.list(dir) devuelve un array de entradas del directorio. fs.mkdir crea un directorio; fs.rmdir elimina un directorio vacío:

Crea 3 archivos en un subdirectorio temporal, lista y cuenta las entradas .txt.

03-list-dir.zolo
Playground
// Feature: fs.list — list contents of a directory

// When to use: walk files in a folder, generate indexes.


use std::fs

// Create 3 files inside a temp sub-directory to list.

let base = fs.temp_dir() + "/zolo-fs-list"
fs.mkdir(base)

fs.write(base + "/a.txt", "1")
fs.write(base + "/b.txt", "2")
fs.write(base + "/c.txt", "3")

// list returns an array of paths/entries.

let entries = fs.list(base)
print(entries.len() >= 3)  // expected: true


// Count how many end with .txt.

var txts = 0
for e in entries {
  if e.ends_with(".txt") {
    txts = txts + 1
  }
}
print(txts >= 3)  // expected: true


// Cleanup.

fs.remove(base + "/a.txt")
fs.remove(base + "/b.txt")
fs.remove(base + "/c.txt")
fs.rmdir(base)
print(fs.exists(base))  // expected: false

Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.


Archivos y directorios temporales

fs.temp_dir() devuelve el directorio temporal del sistema. fs.temp_file(prefix) reserva un nombre único que aún no existe, adecuado para archivos de borrador, cachés y pruebas:

Usa temp_dir y temp_file para crear y eliminar un archivo de borrador.

04-temp-files.zolo
Playground
// Feature: fs.temp_dir / fs.temp_file — temporary scratch areas

// When to use: caches, scratch, tests — without polluting the project tree.


use std::path
use std::fs

// temp_dir — path to the system temp directory.

let dir = fs.temp_dir()
print(fs.is_dir(dir))  // expected: true

print(dir.len() > 0)  // expected: true


// temp_file(prefix) — a unique unused path.

let path = fs.temp_file("zolo-demo-")
print(path.contains("zolo-demo-"))  // expected: true

print(fs.exists(path))  // expected: false (only the name is reserved)


// Write and remove.

fs.write(path, "scratch")
print(fs.exists(path))  // expected: true

print(fs.read(path))  // expected: scratch


fs.remove(path)
print(fs.exists(path))  // expected: false

Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.

Consulta también

Buscar en Zolo

9 resultados

enespt-br