os

The os module exposes the host environment, process information, signal trapping, file permissions, low-level system info, command-line arguments, and process exit. It is organized into submodules with flat top-level shortcuts for common needs. Import it with import os.

os.env

FunctionSignatureDescription
getos.env.get(key: string) → stringvalue of an environment variable (empty if unset)
setos.env.set(key: string, value: string) → boolset an environment variable
unsetos.env.unset(key: string) → boolremove an environment variable
hasos.env.has(key: string) → booltrue if the variable is defined
allos.env.all() → stringall variables as a JSON object string

os.process

FunctionSignatureDescription
pidos.process.pid() → intcurrent process ID
ppidos.process.ppid() → intparent process ID
uidos.process.uid() → intuser ID
gidos.process.gid() → intgroup ID
exitos.process.exit(code?: int) → voidexit the process with code (default 0)
argsos.process.args() → string[]command-line arguments
cwdos.process.cwd() → stringcurrent working directory
chdiros.process.chdir(dir: string) → boolchange the working directory

os.signal

FunctionSignatureDescription
notifyos.signal.notify() → voidstart trapping SIGINT/SIGTERM/SIGHUP
trapos.signal.trap() → stringname of the last received signal (empty if none)
resetos.signal.reset() → voidclear the recorded last signal

os.permission

FunctionSignatureDescription
readableos.permission.readable(path: string) → booltrue if the path can be opened for reading
writableos.permission.writable(path: string) → booltrue if the path can be opened for writing
executableos.permission.executable(path: string) → booltrue if any execute bit is set
chmodos.permission.chmod(path: string, mode: int) → boolchange file mode bits

os.syscall

FunctionSignatureDescription
archos.syscall.arch() → stringCPU architecture (e.g. “arm64”)
osos.syscall.os() → stringoperating system (e.g. “darwin”)
hostnameos.syscall.hostname() → stringmachine hostname
pagesizeos.syscall.pagesize() → intmemory page size in bytes
numCPUos.syscall.numCPU() → intnumber of logical CPUs

os.args

FunctionSignatureDescription
allos.args.all() → string[]all command-line arguments
getos.args.get(idx: int) → stringargument at idx (empty if out of range)
lenos.args.len() → intnumber of arguments

os.exit

FunctionSignatureDescription
codeos.exit.code(code?: int) → voidexit with code (default 0)
successos.exit.success() → voidexit with code 0
failureos.exit.failure() → voidexit with code 1

Top-level shortcuts

FunctionSignatureDescription
getenvos.getenv(key: string) → stringread an environment variable
setenvos.setenv(key: string, value: string) → boolset an environment variable
hostnameos.hostname() → stringmachine hostname
archos.arch() → stringCPU architecture
platformos.platform() → stringoperating system
exitCodeos.exitCode(code?: int) → voidexit with code (default 0)
pidos.pid() → intcurrent process ID

Example

import os

print(os.platform())                  // darwin
print(os.getenv("HOME"))

// trap shutdown signals in a long-running program
os.signal.notify()
if os.signal.trap() == "interrupt" {
    os.exit.success()
}
Standard library · View as Markdown · llms-full.txt