os – OS utilities

This modules provides various functions adn types that are close to the operating system.

Files and folders example

from os import cwd
from os import which
from os.path import Path

func main():
    if which("ls") is not None:
        print("executable ls found")

    foo = Path("foo")
    foo.mkdir()
    foo.cd()
    print("Current working directory is", cwd())
    bar = Path("bar")
    bar.touch()

    if bar.exists():
        print("bar exists")

    print(f"Files:", Path(".").ls())
    Path("..").cd()
    foo.rm(recursive=True, force=True)

Environment variables example

An example of how to use environment variables.

from os import env
from os import setenv
from os import getenv

func main():
    print("Before:", getenv("FOOBAR", "ho"))
    setenv("FOOBAR", "hi")
    print("After: ", getenv("FOOBAR"))

    for name in slice(env().keys(), 0, 5):
        print(name)

API

class Stdin:

    func get(self) -> i32:
        Returns the next byte from standard input (stdin), or -1 if end of
        input is reached.

    func read(self) -> bytes:
        Reads from standard input until end of file is reached.
class Stdout:

    func put(self, value: u8):
        Writes given byte to standard output (stdout).

    func write(self, data: bytes):
        Writes given bytes to standard output (stdout).

    func write(self, data: bytes, offset: i64, count: i64):
        Writes given bytes to standard output (stdout).

    func flush(self):
        Flush output buffer.
class OsError(Error):
    message: string
class BinaryFile:
    A binary file.

    func __init__(self, path: Path, mode: string = "r"):
        Opens given file. Mode can be `r` or `w`.

    func close(self):
        Close the file. The file is closed automatically in the destructor
        if not already closed.

    func read(self) -> bytes:
        Read from the file.

    func write(self, data: bytes):
        Write to the file.
class TextFile:
    A text file.

    func __init__(self, path: Path, mode: string = "r"):
        Opens given file. Mode can be `r` or `w`.

    func close(self):
        Close the file. The file is closed automatically in the destructor
        if not already closed.

    func read(self) -> string:
        Read from the file.

    func write(self, data: string):
        Write to the file.
class Passwd:
    name: string
    uid: i64
    gid: i64
    dir: string
    shell: string
func which(executable: string) -> Path:
    Locate given executable in the user's path.
func cwd() -> Path:
func env() -> {string: string}:
    Returns all environment variables.
func getenv(name: string, default: string? = None) -> string?:
    Get given environment variable. Returns given default value if the
    environment variable is not set.
func setenv(name: string, value: string):
    Set given environment variable.
func unsetenv(name: string):
    Unset given environment variable.
func tar(archive: Path,
         extract: bool = False,
         create: bool = False,
         strip_components: i64 = 0,
         output_directory: Path? = None,
         path: Path? = None):
func nproc(all: bool = False, ignore: i64 = 0) -> i64:
    Get the number of processing units available.
func geteuid() -> i64:
    Get the effective user ID of the calling process.
func getpasswd(uid: i64) -> Passwd:
    Get given user's passwd entry.
func getpasswd(username: string) -> Passwd:
    Get given user's passwd entry.
func whoami() -> string:
    Get effective user name.
STDIN: Stdin
STDOUT: Stdout