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.type stringExample,
%> import os %> os.platform 'osx'
- os.args
- A list containing the command line arguments passed to the startup script. type list
- os.path_separator
- The standard path separator for the current operating system. type string
- os.exe_path
- The full path to the running Blade executable. type string
- os.DT_UNKNOWN
- Unknown file type type number
- os.DT_BLK
- Block device file type type number
- os.DT_CHR
- Character device file type type number
- os.DT_DIR
- Directory file type type number
- os.DT_FIFO
- Named pipe file type type number
- os.DT_LNK
- Symbolic link file type type number
- os.DT_REG
- Regular file type type number
- os.DT_SOCK
- Local-domain socket file type type number
- os.DT_WHT
- Whiteout file type (only meaningful on UNIX and some unofficial Linux versions). type number
- value is
-1
on systems where it is not supported.
- value is
- os.current_file
- current_file()
A string containing the path to the current file from which the value is accessed.
type string
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 systemnodename
The name of the current machineversion
: The operating system versionrelease
: The release level/versionmachine
: The hardware/processor type.
return dictExample,
%> 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 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'
- 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 returnstrue
. - permission should be given as octal number.
return boolean - if the directory already exists, it returns
- 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 orfalse
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 orfalse
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 - if the path is a file, see
- 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. Ifpath
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