Skip to content
Docs
Modules
os

os

Module os provides a platform-independent interface to operating system functionality.

By default, this module interacts with the host operating system normally by calling the underlying Go os package. However, alternative OS abstraction layers may be used via the Go WithOS (opens in a new tab) function. This assists with sandboxing scripts and providing access to object storage like AWS S3 via a filesystem-like interface.

Attributes

stdin

stdin is an open file pointing to the standard input for the process.

Example
>>> os.stdin.read()
byte_slice("hello world")

stdout

stdout is an open file pointing to the standard output for the process.

Example
>>> os.stdout.write("hello world")
11

Functions

chdir

Function signature
chdir(dir string)

Changes the working directory to dir.

Example
>>> os.chdir("/tmp")
>>> os.getwd()
"/tmp"

create

Function signature
create(name string) File

Creates or truncates the named file.

Example
>>> f := os.create("foo.txt")
>>> f.write("hello world")
11
>>> f.close()

environ

Function signature
environ() list

Returns a copy of strings representing the environment, in the form "key=value".

Example
>>> os.environ()
["TERM=xterm-256color", "SHELL=/bin/bash", "USER=alice", ...]

exit

Function signature
exit(code int)

Terminates the program with the given exit code.

Example
>>> os.exit(0)

getenv

Function signature
getenv(key string) string

Returns the value of the environment variable key.

Example
>>> os.getenv("USER")
"alice"

getpid

Function signature
getpid() int

Returns the current process ID.

Example
>>> os.getpid()
1234

getuid

Function signature
getuid() int

Returns the current user ID.

Example
>>> os.getuid()
501

getwd

Function signature
getwd() string

Returns the current working directory.

Example
>>> os.getwd()
"/home/alice"

hostname

Function signature
hostname() string

Returns the host name reported by the kernel.

Example
>>> os.hostname()
"alice-macbook-pro-1.local"

mkdir_all

Function signature
mkdir_all(path string, perm int)

Creates a directory named path, along with any necessary parent directories.

Example
>>> os.mkdir_all("/tmp/foo/bar", 0755)

mkdir_temp

Function signature
mkdir_temp(dir, prefix string) string

Creates a new temporary directory in the directory dir, using prefix to generate its name.

Example
>>> os.mkdir_temp("/tmp", "foo")
"/tmp/foo4103914411"

mkdir

Function signature
mkdir(path string, perm int)

Creates a new directory with the specified name and permission bits. If a permissions value is not specified, 0755 is used.

Example
>>> os.mkdir("/tmp/foo", 0755)

open

Function signature
open(name string) File

Opens the named file.

Example
>>> f := os.open("foo.txt")
>>> f.read()
byte_slice("hello world")
>>> f.close()

read_dir

Function signature
read_dir(name string) list

Returns a list of directory entries sorted by filename. If a name is not specified, the current directory is used.

Example
>>> os.read_dir("/tmp")
[dir_entry(name=foo.txt, type=regular), dir_entry(name=bar.txt, type=regular)]

read_file

Function signature
read_file(name string) byte_slice

Reads the named file and returns its contents.

Example
>>> os.read_file("/tmp/foo.txt")
byte_slice("hello world")

remove

Function signature
remove(name string)

Removes the named file or empty directory.

Example
>>> os.remove("/tmp/old/junk.txt")

remove_all

Function signature
remove_all(name string)

Removes path and any children it contains.

Example
>>> os.remove_all("/tmp/junk")

rename

Function signature
rename(old, new string)

Renames (moves) old to new.

Example
>>> os.rename("old.txt", "new.txt")

setenv

Function signature
setenv(key, value string)

Sets the value of the environment variable key to value.

Example
>>> os.setenv("USER", "bob")
>>> os.getenv("USER")
"bob"

stat

Function signature
stat(name string) FileInfo

Returns a FileInfo describing the named file.

Example
>>> os.stat("example.txt")
file_info(name=example.txt, mode=-rw-r--r--, size=84, mod_time=2023-08-06T08:45:56-04:00)

symlink

Function signature
symlink(old, new string)

Creates a symbolic link new pointing to old.

Example
>>> os.symlink("foo.txt", "bar.txt")

temp_dir

Function signature
temp_dir() string

Returns the default directory to use for temporary files.

Example
>>> os.temp_dir()
"/tmp"

unsetenv

Function signature
unsetenv(key string)

Unsets the environment variable key.

Example
>>> os.unsetenv("USER")
>>> os.getenv("USER")
""

user_cache_dir

Function signature
user_cache_dir() string

Returns the default root directory to use for user-specific non-essential data.

Example
>>> os.user_cache_dir()
"/home/alice/.cache"

user_config_dir

Function signature
user_config_dir() string

Returns the default root directory to use for user-specific configuration data.

Example
>>> os.user_config_dir()
"/home/alice/.config"

user_home_dir

Function signature
user_home_dir() string

Returns the current user's home directory.

Example
>>> os.user_home_dir()
"/home/alice"

write_file

Function signature
write_file(name string, data byte_slice / string)

Writes the given byte_slice or string to the named file.

Example
>>> os.write_file("example.txt", "hey!")
>>> os.read_file("example.txt")
byte_slice("hey!")