OS
@std/os manages files and directories on the host filesystem. For reading or writing file contents, use IO. For process information and the working directory, use System.
import os from "@std/os"
let entries = os.readDir(".")
for entry in entries { println(entry.name, entry.isDir)}Functions
Section titled “Functions”| Function | Returns | Behavior |
|---|---|---|
readDir(dir: string) | []DirEntry | Read directory entries, sorted by filename. |
copy(src: string, dst: string) | void | Copy a file’s bytes, creating or truncating the destination. |
move(src: string, dst: string) | void | Rename or move a path using the host OS. |
remove(path: string) | void | Remove a file or directory tree recursively. |
exists(path: string) | bool | True when the path can be stat’ed successfully. |
mkdir(path: string) | void | Create directories recursively, requesting mode 0755. |
Paths are interpreted by the host OS relative to the process working directory, not automatically relative to __file__. Copying does not recursively copy directories or preserve all metadata. Moving does not create destination parents and may fail across filesystems.
These functions generally raise catchable Nubo errors on host failures. exists instead returns false for any failed stat, including permission errors; it is not a detailed diagnostic.
Directory metadata
Section titled “Directory metadata”DirEntry has these members:
| Member | Type | Meaning |
|---|---|---|
name | string | Entry basename. |
isDir | bool | Whether this entry is a directory. |
info() | FileInfo | Load its file metadata; this call can fail independently. |
FileInfo contains:
| Field | Type | Meaning |
|---|---|---|
name | string | Basename. |
isDir | bool | Directory flag. |
size | int | Size in bytes. |
mode | int | Host file mode, including its mode bits. |
modTime | Time | Time instance supporting format(...). |
import os from "@std/os"
let entries = os.readDir(".")
for entry in entries { let info = entry.info()
println(entry.name, info.size, info.modTime.format("Y-m-d H:i:s"))}Copy and move a file
Section titled “Copy and move a file”This example creates its source file before copying it and creates the destination directory before moving it:
import io from "@std/io"import os from "@std/os"import system from "@std/system"
let folder = system.cwd() + "/nubo-demo"
os.mkdir(folder + "/archive")io.writeFile(folder + "/input.txt", "Hello, Nubo")
os.copy(folder + "/input.txt", folder + "/backup.txt")os.move(folder + "/backup.txt", folder + "/archive/backup.txt")
println(os.exists(folder + "/archive/backup.txt"))To remove the example directory and everything inside it:
import os from "@std/os"
os.remove("nubo-demo")Handle a filesystem error
Section titled “Handle a filesystem error”import os from "@std/os"
catch err { let entries = os.readDir("directory-that-does-not-exist") println(entries)}
if !isNil(err) { println(err.message)}Choosing nubo.NewWithFS(...) in Go changes script loading. It does not redirect this package’s host filesystem operations.