Next | Previous

os

This module provides functions for interfacing with the underlying operating system and directories.

Fields

os.platformstring

The name of the current platform in string or unknown if the platform name could not be determined.

Example,

%> import os
%> os.platform
'osx'
os.argslist

A list containing the command line arguments passed to the startup script.

os.path_separatorstring

The standard path separator for the current operating system.

os.exe_pathstring

The full path to the running Blade executable.

os.DT_UNKNOWNnumber

Unknown file type

os.DT_BLKnumber

Block device file type

os.DT_CHRnumber

Character device file type

os.DT_DIRnumber

Directory file type

os.DT_FIFOnumber

Named pipe file type

os.DT_LNKnumber

Symbolic link file type

os.DT_REGnumber

Regular file type

os.DT_SOCKnumber

Local-domain socket file type

os.DT_WHTnumber

Whiteout file type (only meaningful on UNIX and some unofficial Linux versions).

Functions

os.exec(cmd)

Executes the given shell (or command prompt for Windows) commands and returns the output as string.

Example,

%> os.exec('ls -l')
'total 48
-rw-r--r--@ 1 username  staff  705 Aug 27  2021 buggy.b
-rw-r--r--  1 username  staff  197 Mar  5 05:13 myprogram.b'
  • @params:
    • string cmd
  • @returns: string
os.info()

Returns information about the current operation system and machine as a dictionary. The returned dictionary will contain:

  • sysname: The name of the operating system
  • nodename The name of the current machine
  • version: The operating system version
  • release: The release level/version
  • machine: The hardware/processor type.

Example,

%> os.info()
{sysname: Darwin, nodename: MacBook-Pro.local, version: Darwin Kernel Version 
21.1.0: Wed Oct 13 17:33:24 PDT 2021; root:xnu-8019.41.5~1/RELEASE_ARM64_T8101, 
release: 21.1.0, machine: arm64}
  • @returns: dict
os.sleep(duration)

Causes the current thread to sleep for the specified number of seconds.

  • @params:
    • number duration
os.get_env(name)

Returns the given environment variable if exists or nil otherwise

Example,

%> import os
%> os.get_env('ENV1')
'20'
  • @params:
    • string name
  • @returns: string
os.set_env(name, value, overwrite)

Sets the named environment variable to the given value.

Example,

%> os.set_env('ENV1', 'New value')
true
%> os.get_env('ENV1')
'New value'

If you are in the REPL and have tried the last example in get_env(), you may notice that the value of ENV1 doesn't change. This is because unless you specify, set_env() will not overwrite existing environment variables. For that, you will need to specify true as the third parameter to set_env().

For example,

%> os.set_env('ENV1', 'New value again', true)
true
%> os.get_env('ENV1')
'New value again'

@notes:

  • Environment variables set will not persist after application exists.
  • @params:
    • string name
    • string value
    • bool? overwrite Default value is false.
  • @returns: bool
os.create_dir(path, permission, recursive)

Creates the given directory with the specified permission and optionally add new files into it if any is given.

@notes:

  • if the directory already exists, it returns false otherwise, it returns true.
  • permission should be given as octal number.
  • @params:
    • string path

    • number? permission Default value is 0c777

    • bool? recursive Default value is true.

  • @returns: boolean
os.read_dir(path)

Scans the given directory and returns a list of all matched files

Example,

%> os.read_dir('./tests')
[., .., myprogram.b, single_thread.b, test.b, buggy.b]

@notes:

  • . indicates current directory and can be used as argument to os.path as well.
  • .. indicates parent directory and can be used as argument to os.path as well.
  • @params:
    • string path
  • @returns: list[string]
os.chmod(path, mode)

Changes the permission set on a directory to the given mode. It is advisable to set the mode with an octal number (e.g. 0c777) as this is consistent with operating system values.

  • @params:
    • string path
    • number mode
  • @returns: boolean
os.is_dir(path)

Returns true if the path is a directory or false otherwise.

  • @params:
    • string path
  • @returns: bool
os.remove_dir(path, recursive)

Deletes a non-empty directory. If recursive is true, non-empty directories will have their contents deleted first.

  • @params:
    • string path
    • bool recursive Default value is false.
  • @returns: bool
os.cwd()

Returns the current working directory.

  • @returns: string
os.change_dir(path)

Navigates the working directory into the specified path.

  • @params:
    • string path
  • @returns: bool
os.dir_exists(path)

Returns true if the directory exists or false otherwise.

  • @params:
    • string path
  • @returns: bool
os.exit(code)

Exit the current process and quits the Blade runtime.

  • @params:
    • number code
os.join_paths(...)

Concatenates the given paths together into a format that is valid on the current operating system.

Example,

%> os.join_paths('/home/user', 'path/to/myfile.ext')
'/home/user/path/to/myfile.ext'
  • @params:
    • string... paths
  • @returns: string
os.real_path(path)

Returns the original path to a relative path.

@notes:

  • if the path is a file, see abs_path().
  • @params:
    • string path
  • @returns: string
os.abs_path(path)

Returns the original path to a relative path.

@notes:

  • unlike real_path(), this function returns full path for a file.
  • @params:
    • string path
  • @returns: string
os.dir_name(path)

Returns the parent directory of the pathname pointed to by path. Any trailing / characters are not counted as part of the directory name. If path is an empty string, or contains no / characters, dir_name() returns the string ".", signifying the current directory.

  • @params:
    • string path
  • @returns: string
os.base_name(path)

The base_name() function returns the last component from the pathname pointed to by path, deleting any trailing / characters. If path consists entirely of / characters, the string '/' is returned. If path is an empty string, the string '.' is returned.

  • @params:
    • string path
  • @returns: string