Link Search Menu Expand Document

os

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

Properties


os.platform
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.args
A list containing the command line arguments passed to the startup script.
os.path_separator
The standard path separator for the current operating system.

Functions


os.exec(cmd: string)
Executes the given shell (or command prompt for Windows) commands and returns the output as string.
return 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'
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.
return dict

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}
os.sleep(duration: number)
Causes the current thread to sleep for the specified number of seconds.
os.get_env(name: string)
Returns the given environment variable if exists or nil otherwise
return string

Example,

%> import os
%> os.get_env('ENV1')
'20'
os.set_env(name: string, value: string, overwrite: bool = true)
Sets the named environment variable to the given value.
return string

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'
  • Environment variables set will not persist after application exists.
os.create_dir(path: string, [permission: number = 0c777 [, recursive: boolean = true]])
Creates the given directory with the specified permission and optionaly add new files into it if any is given.
  • if the directory already exists, it returns false otherwise, it returns true.
  • permission should be given as octal number.
return boolean
os.read_dir(path: string)
Scans the given directory and returns a list of all matched files
return list[string]

Example,

%> os.read_dir('./tests')
[., .., myprogram.b, single_thread.b, test.b, buggy.b]
  • . 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.
os.chmod(path: string, mod: number)
Changes the permission set on a directory
  • mod should be octal number (e.g. 0c777)
return boolean
os.is_dir(path: string)
Returns true if the path is a directory or false otherwise.
return bool
os.remove_dir(path: string [, recursive: boolean = false])
Deletes a non-empty directory. If recursive is true, non-empty directories will have their contents deleted first.
return bool
os.cwd()
Returns the current working directory
return string
os.change_dir(path: string)
Navigates the working directory into the specified path.
return bool
os.exists(path: string)
Returns true if the directory exists or false otherwise.
return bool
os.exit(code: number)
Exit the current process and quits the Blade runtime.
return
os.join_paths(paths…)
Concatenates the given paths together into a format that is valied on the current operating system.
return string

Example,

%> os.join_paths('/home/user', '/path/to/myfile.ext')
'/home/user/path/to/myfile.ext'
os.real_path(path: string)
returns the original path to a relative path.
  • if the path is a file, see abs_path()
return string
os.abs_path(path: string)
returns the original path to a relative path.
  • unlike real_path(), this function returns full path for a file
return string
os.dir_name(path: string)
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.
return string
os.base_name(path: string)
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.
return string

Back to top

Copyright © 2021 Ore Richard Muyiwa. Distributed under the MIT license.