Standard library
std/os
Files, directories, the environment and processes.
let text = try os.readText("notes.txt")
for name in try os.readDir(".") {
println(name)
}
let r = try os.run("git", ["status", "--short"])
print(r.stdout)Types: type Error, struct File, enum FileKind, struct Info, struct Output
Functions: args, exit, open, create, append, stdin, stdout, stderr, readFile, readText, writeFile, writeText, stat, lstat, exists, readDir, mkdir, mkdirAll, remove, removeAll, rename, cwd, chdir, tempDir, joinPath, dirName, baseName, getenv, setenv, unsetenv, run, runWithInput, call
Types
type Error
pub type Error = io.ErrorErrors here are io.Error (with an io.ErrorKind to match on).
struct File
pub struct File {
// and private fields
}An open file. It is closed when it goes out of scope.
File.read
pub fn read(self, buf: inout Vec[byte], max: int) -> int ! ErrorAppends at most max bytes to buf; returns how many, 0 at the end.
File.write
pub fn write(self, data: []byte) ! ErrorWrites all of data.
File.writeText
pub fn writeText(self, s: str) ! ErrorWrites the bytes of s.
File.readAll
pub fn readAll(self) -> Vec[byte] ! ErrorReads to the end.
File.readText
pub fn readText(self) -> String ! ErrorReads to the end, as a string.
File.flush
pub fn flush(self)Writes out what print and the standard streams have buffered.
enum FileKind
pub copy enum FileKind { File, Directory, Symlink, Other }What a path names. (Other: a device, socket or pipe.)
struct Info
pub struct Info {
pub kind: FileKind
pub size: int
// last modification, in nanoseconds since 1970 (`time.local(info.modified)`)
pub modified: int
// permission bits, like 0o644
pub mode: int
}What stat reports about a file.
Info.isDir
pub fn isDir(self) -> boolWhether it is a directory.
Info.isFile
pub fn isFile(self) -> boolWhether it is a regular file.
struct Output
pub struct Output {
// the exit code, or 128 + the signal that ended it
pub status: int
pub stdout: String
pub stderr: String
}What a finished process printed, and how it ended.
Output.ok
pub fn ok(self) -> boolWhether the process exited with status 0.
Functions
args
pub fn args() -> Vec[String]The command line: the program's name, then its arguments.
exit
pub fn exit(code: int) -> neverEnds the program with an exit code, after flushing standard output. Deferred code does not run.
open
pub fn open(path: str) -> File ! ErrorOpens a file for reading.
create
pub fn create(path: str) -> File ! ErrorCreates a file for writing, or empties an existing one.
append
pub fn append(path: str) -> File ! ErrorOpens a file for writing at its end, creating it if needed.
stdin, stdout, stderr
pub fn stdin() -> File
pub fn stdout() -> File
pub fn stderr() -> FileThe standard streams. They are not closed when the File is dropped.
readFile
pub fn readFile(path: str) -> Vec[byte] ! ErrorThe whole contents of a file.
readText
pub fn readText(path: str) -> String ! ErrorThe whole contents of a file, as a string (the bytes aren't checked to be UTF-8).
writeFile
pub fn writeFile(path: str, data: []byte) ! ErrorCreates or replaces a file with data. Not atomic: write to a temporary
name and rename it into place for that.
writeText
pub fn writeText(path: str, text: str) ! ErrorCreates or replaces a file with the bytes of text.
stat
pub fn stat(path: str) -> Info ! ErrorWhat path is; follows symbolic links.
lstat
pub fn lstat(path: str) -> Info ! ErrorLike stat, but describes a symbolic link itself.
exists
pub fn exists(path: str) -> boolWhether path names something (following symbolic links). To act on a
file, just try it and handle the error: it may change in between.
readDir
pub fn readDir(path: str) -> Vec[String] ! ErrorThe names of the entries in a directory, sorted.
mkdir
pub fn mkdir(path: str) ! ErrorCreates a directory; fails if it exists or its parent doesn't.
mkdirAll
pub fn mkdirAll(path: str) ! ErrorCreates a directory and any missing parents; fine if it exists already.
remove
pub fn remove(path: str) ! ErrorRemoves a file or an empty directory.
removeAll
pub fn removeAll(path: str) ! ErrorRemoves a file, or a directory and everything in it. Symbolic links are removed, not followed.
rename
pub fn rename(from: str, to: str) ! ErrorRenames or moves a file or directory, replacing to if it is a file. Both
must be on the same file system.
cwd
pub fn cwd() -> String ! ErrorThe working directory.
chdir
pub fn chdir(path: str) ! ErrorChanges the working directory, for the whole process.
tempDir
pub fn tempDir() -> StringThe directory for temporary files ($TMPDIR, or /tmp).
joinPath
pub fn joinPath(a: str, b: str) -> String"a/b" + "c" = "a/b/c".
dirName
pub fn dirName(path: str) -> StringThe directory part of a path: "a/b/c" -> "a/b", "c" -> ".", "/c" -> "/".
baseName
pub fn baseName(path: str) -> StringThe last part of a path: "a/b/c.txt" -> "c.txt".
getenv
pub fn getenv(name: str) -> ?StringThe value of an environment variable.
setenv
pub fn setenv(name: str, value: str) ! ErrorSets an environment variable, for this process and the programs it starts.
unsetenv
pub fn unsetenv(name: str) ! ErrorRemoves an environment variable.
run
pub fn run(prog: str, args: []String) -> Output ! ErrorRuns a program (looked up in $PATH) and waits for it, collecting what it
prints. Fails only if it can't be started: check status for how it ended.
runWithInput
pub fn runWithInput(prog: str, args: []String, input: str) -> Output ! ErrorLike run, with input as the program's standard input.
call
pub fn call(prog: str, args: []String) -> int ! ErrorRuns a program on this process's standard streams and returns its exit code.