os
This module provides functions for interfacing with the underlying operating system and directories.
Fields
- os.platform ➝ string
-
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 ➝ list
-
A list containing the command line arguments passed to the startup script.
- os.path_separator ➝ string
-
The standard path separator for the current operating system.
- os.exe_path ➝ string
-
The full path to the running Blade executable.
- os.DT_UNKNOWN ➝ number
-
Unknown file type
- os.DT_BLK ➝ number
-
Block device file type
- os.DT_CHR ➝ number
-
Character device file type
- os.DT_DIR ➝ number
-
Directory file type
- os.DT_FIFO ➝ number
-
Named pipe file type
- os.DT_LNK ➝ number
-
Symbolic link file type
- os.DT_REG ➝ number
-
Regular file type
- os.DT_SOCK ➝ number
-
Local-domain socket file type
- os.DT_WHT ➝ number
-
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
- @params:
- 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 systemnodename
The name of the current machineversion
: The operating system versionrelease
: The release level/versionmachine
: 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
- @params:
- 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
- @params:
- 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 ofENV1
doesn't change. This is because unless you specify,set_env()
will not overwrite existing environment variables. For that, you will need to specifytrue
as the third parameter toset_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 returnstrue
.
- permission should be given as octal number.
- @params:
-
string path
-
number? permission Default value is
0c777
-
bool? recursive Default value is
true
.
-
- @returns: boolean
- if the directory already exists, it returns
- 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
- @params:
- os.is_dir(path)
-
Returns
true
if the path is a directory orfalse
otherwise.- @params:
- string path
- @returns: bool
- @params:
- 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
- @params:
- 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
- @params:
- os.dir_exists(path)
-
Returns
true
if the directory exists orfalse
otherwise.- @params:
- string path
- @returns: bool
- @params:
- os.exit(code)
-
Exit the current process and quits the Blade runtime.
- @params:
- number code
- @params:
- 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
- @params:
- 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
- if the path is a file, see
- 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. Ifpath
is an empty string, or contains no/
characters, dir_name() returns the string ".", signifying the current directory.- @params:
- string path
- @returns: string
- @params:
- 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
- @params: