Next | Previous

os

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

Properties

  • 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'
  • argslist:

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

  • path_separatorstring:

    The standard path separator for the current operating system.

  • exe_pathstring:

    The full path to the running Blade executable.

  • DT_UNKNOWNnumber:

    Unknown file type

  • DT_BLKnumber:

    Block device file type

  • DT_CHRnumber:

    Character device file type

  • DT_DIRnumber:

    Directory file type

  • DT_FIFOnumber:

    Named pipe file type

  • DT_LNKnumber:

    Symbolic link file type

  • DT_REGnumber:

    Regular file type

  • DT_SOCKnumber:

    Local-domain socket file type

  • DT_WHTnumber:

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

Functions

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'
Parameters
  • string cmd
Returns
  • string

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

sleep(duration)

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

Parameters
  • number duration

get_env(name)

Returns the given environment variable if exists or nil otherwise

Example,

%> import os
%> os.get_env('ENV1')
'20'
Parameters
  • string name
Returns
  • string

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'
Parameters
  • string name
  • string value
  • bool? overwrite: : Default value is false.
Returns
  • string
Notes
  • Environment variables set will not persist after application exists.

create_dir(path, permission, recursive)

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

Parameters
  • string path
  • number? permission: : Default value is 0c777
  • bool? recursive: : Default value is true.
Returns
  • boolean
Notes
  • if the directory already exists, it returns false otherwise, it returns true.
  • permission should be given as octal number.

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]
Parameters
  • string path
Returns
  • list[string]
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.

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.

Parameters
  • string path
  • number mode
Returns
  • boolean

is_dir(path)

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

Parameters
  • string path
Returns
  • bool

remove_dir(path, recursive)

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

Parameters
  • string path
  • bool recursive: : Default value is false.
Returns
  • bool

cwd()

Returns the current working directory.

Returns
  • string

change_dir(path)

Navigates the working directory into the specified path.

Parameters
  • string path
Returns
  • bool

dir_exists(path)

Returns true if the directory exists or false otherwise.

Parameters
  • string path
Returns
  • bool

exit(code)

Exit the current process and quits the Blade runtime.

Parameters
  • number code

join_paths(...)

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

Example,

%> os.join_paths('/home/user', 'path/to/myfile.ext')
'/home/user/path/to/myfile.ext'
Parameters
  • string... paths
Returns
  • string

real_path(path)

Returns the original path to a relative path.

Parameters
  • string path
Returns
  • string
Notes
  • if the path is a file, see abs_path().

abs_path(path)

Returns the original path to a relative path.

Parameters
  • string path
Returns
  • string
Notes
  • unlike real_path(), this function returns full path for a file.

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.

Parameters
  • string path
Returns
  • string

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.

Parameters
  • string path
Returns
  • string