os
Files, directories, system commands, program arguments and environment variables.
import os
The os package is the simplest way to talk to the system: each function does one thing, and most of them take a file or directory path as a string. The functions that change something, such as Remove and Mkdir, return zero on success and some other number otherwise.
There are sub-packages for more specialised work: os.process for running other programs, os.shell for capturing command output, os.platform for identifying the system, and the fs.file and fs.dir packages, which do more.
Types
FileInfo
struct FileInfoStat returns.| Name | Type | Description |
|---|---|---|
size | i64 = 0 | The size of the file in bytes. |
is_dir | bool = false | True when the path is a directory. |
exists | bool = false | True when the path exists. |
import os
func main:
os.WriteFile("note.txt", "first line\n")
os.AppendFile("note.txt", "second line\n")
print os.ReadFile("note.txt")
println os.Exists("note.txt"), os.FileSize("note.txt")
info := os.Stat("note.txt")
println info.exists, info.is_dir, info.size
os.Copy("note.txt", "copy.txt")
os.Move("copy.txt", "new name.txt")
println os.Exists("copy.txt"), os.Exists("new name.txt")
os.Remove("new name.txt")
println os.Exists("new name.txt")
f := os.Open("note.txt", "rb")
println f.readline()
f.close()
endCmdResult
struct CmdResultRunCapture.| Name | Type | Description |
|---|---|---|
code | int = 0 | The command's exit code; zero means success. |
out | str = "" | The command's output. |
import os
func main:
code := os.Run("echo from the shell")
println "exit code:", code
r := os.RunCapture("echo hello")
println r.code, "[" + r.out + "]"
println "[" + os.Output("echo world") + "]"
println os.Pid() > 0
endFunctions
Open
func Open(path: str, mode: str): File*"rb" to read, "wb" to rewrite and "ab" to append. It returns null when it cannot.The example for this item is shown with the ones above: os-Files.salam
Exit
func Exit(code: int)main write ret instead, because exiting does not run the defer statements (error E109).import os
func fail(message: str):
printerrln message
os.Exit(2)
end
func give_up():
os.Abort()
end
func main:
args := os.Args()
println "argument count:", args.len()
home := os.Env("HOME")
println home.len() > 0 or os.Env("USERPROFILE").len() > 0
if args.len() > 10:
fail("too many arguments")
end
if args.len() > 100:
give_up()
end
endAbort
func Abort()The example for this item is shown with the ones above: os-Args.salam
Args
func Args(): Vector<str>The example for this item is shown with the ones above: os-Args.salam
Env
func Env(name: str): strThe example for this item is shown with the ones above: os-Args.salam
Remove
func Remove(path: str): intThe example for this item is shown with the ones above: os-Files.salam
Mkdir
func Mkdir(path: str): intimport os
func main:
os.Mkdir("project")
os.MkdirAll("project/src/helpers")
os.WriteFile("project/readme.txt", "hello")
os.WriteFile("project/src/main.salam", "func main: end")
println os.IsDir("project"), os.IsDir("project/readme.txt")
println os.ListDir("project/src").len(), os.ListDirs("project")
println os.Walk("project").len()
os.CopyTree("project", "backup")
println os.Exists("backup/src/main.salam")
start := os.Cwd()
os.Chdir("project")
println os.Exists("readme.txt")
os.Chdir(start)
os.Rmdir("project/src/helpers")
os.RemoveAll("project")
os.RemoveAll("backup")
println os.Exists("project"), os.TempDir().len() > 0
endRmdir
func Rmdir(path: str): intThe example for this item is shown with the ones above: os-Dirs.salam
Copy
func Copy(src: str, dst: str): intThe example for this item is shown with the ones above: os-Files.salam
Move
func Move(src: str, dst: str): intThe example for this item is shown with the ones above: os-Files.salam
ListDir
func ListDir(path: str): Vector<str>The example for this item is shown with the ones above: os-Dirs.salam
IsDir
func IsDir(path: str): boolThe example for this item is shown with the ones above: os-Dirs.salam
Walk
func Walk(root: str): Vector<str>The example for this item is shown with the ones above: os-Dirs.salam
Exists
func Exists(path: str): boolThe example for this item is shown with the ones above: os-Files.salam
Cwd
func Cwd(): strThe example for this item is shown with the ones above: os-Dirs.salam
Chdir
func Chdir(path: str): intThe example for this item is shown with the ones above: os-Dirs.salam
TempDir
func TempDir(): str/tmp.The example for this item is shown with the ones above: os-Dirs.salam
ReadFile
func ReadFile(path: str): strThe example for this item is shown with the ones above: os-Files.salam
WriteFile
func WriteFile(path: str, data: str): intThe example for this item is shown with the ones above: os-Files.salam
AppendFile
func AppendFile(path: str, data: str): intThe example for this item is shown with the ones above: os-Files.salam
FileSize
func FileSize(path: str): i64The example for this item is shown with the ones above: os-Files.salam
Stat
func Stat(path: str): FileInfoexists is false.The example for this item is shown with the ones above: os-Files.salam
MkdirAll
func MkdirAll(path: str): intThe example for this item is shown with the ones above: os-Dirs.salam
RemoveAll
func RemoveAll(path: str): intThe example for this item is shown with the ones above: os-Dirs.salam
CopyTree
func CopyTree(src: str, dst: str): intThe example for this item is shown with the ones above: os-Dirs.salam
ListDirs
func ListDirs(path: str): Vector<str>The example for this item is shown with the ones above: os-Dirs.salam
Run
func Run(cmd: str): intThe example for this item is shown with the ones above: os-Run.salam
RunCapture
func RunCapture(cmd: str): CmdResultCmdResult.The example for this item is shown with the ones above: os-Run.salam
Output
func Output(cmd: str): strThe example for this item is shown with the ones above: os-Run.salam
Pid
func Pid(): intThe example for this item is shown with the ones above: os-Run.salam