Operating System¶
This page documents functions that let you interact with the user’s operating system, and perform operations such as checking whether a file or directory exists, extracting parts of a file name, etc. All these functions accept and return UTF-8 paths, which are resolved correctly on Linux, macOS and Windows alike.
Functions¶
- get_user_directory()¶
Get the user’s directory (read only).
- get_current_directory()¶
Get the current directory.
set_current_directory("/home/julien/Documents")
print(get_current_directory())
- set_current_directory(path as String)¶
Set the current directory to path.
- get_full_path(relative_path as String)¶
Turn a relative path into an absolute path.
- get_path_separator()¶
Get the native path separator on the current platform. This is \\ on Windows and / on other platforms.
- get_os_name()¶
Return the name of the current platform: "windows", "macos" or "linux".
- get_script_path()¶
Return the path of the script file whose code is currently running, so a script can locate data relative to itself. If no script file is associated with the running code (for instance in the console), an error is raised.
- get_temp_directory()¶
Get the name of a directory where temporary files can be written.
- get_temp_name()¶
Return the name of a temporary file which is guaranteed to be unique.
- get_base_name(path as String)¶
Return the file name part of path, stripping all directories.
print(get_base_name("/home/hodor/test.txt")) # prints "test.txt"
- get_directory(path as String)¶
Return the directory part of path, stripping the file name.
- create_directory(path as String)¶
Create a new directory. If the directory could not be created, an error is thrown.
- remove_directory(path as String)¶
Remove a directory, which must be empty. If the directory could not be removed, an error is thrown.
- remove_directory(path as String, recursive as Boolean)¶
Remove a directory. If recursive is true, the content is removed recursively; otherwise, the directory is assumed to be empty. If the directory could not be removed, an error is thrown.
- remove_file(path as String)¶
Remove a file. If the file could not be removed, an error is thrown.
- remove_path(path as String)¶
Remove path, which can be either a file or a directory. If path could not be removed, an error is thrown.
- list_directory(path as String)¶
Return a list containing the names of the files in path. Hidden files are not included.
- list_directory(path as String, include_hidden as Boolean)¶
Return a list containing the names of the files in path. If include_hidden is true, hidden files are included. If it is false or is missing,
hidden files are not included.
- exists(path as String)¶
Return true if the path exists, false otherwise.
- is_document(path as String)¶
Return true if path exists and is a file, false otherwise.
- is_directory(path as String)¶
Return true if path exists and is a directory, false otherwise.
- get_extension(path as String)¶
Get the file’s extension, starting with a dot.
- get_extension(path as String, lower as Boolean)¶
Get the file’s extension, starting with a dot. If lower is true, the extension is converted to lower case.
- strip_extension(path as String)¶
Return path without extension.
- split_extension(path as String)¶
Return a list whose first element is path with the extension removed, and whose second element is the extension.
- join_path(s1 as String, s2 as String)¶
Concatenate s1 and s2 using the native path separator and returns the resulting path name.
- genericize(path as String)¶
On Windows, this function converts the native path separator to the generic separator /. On platforms that use the generic separator, it does nothing.
- nativize(path as String)¶
On Windows, this function converts the generic path separator to the native separator \\. On platforms that use the generic separator, it does nothing.
- rename(old_name as String, new_name as String)¶
Renames a file. If the file could not be renamed, an error is raised.