path
The path package defines Path, an object wrapping a filesystem path, in the spirit of Python’s pathlib. Create one with Path.new(text) or text.to_path(), and join paths with /.
report = Path.new("/home/user/docs/report.txt")
println report.name() # report.txtprintln report.ext() # .txtprintln report.parent().to_string() # /home/user/docsprintln (report.parent() / "notes.txt").to_string() # /home/user/docs/notes.txtprintln report.parts() # ["home", "user", "docs", "report.txt"]println report.contains("docs") # true
println "/tmp".to_path().joinpath(["a", "b.txt"]).to_string() # /tmp/a/b.txt/ and joinpath accept strings or other Path values. Methods that return a path return a new Path; call to_string() to get the text.
Filesystem operations
Section titled “Filesystem operations”scratch = Path.temp_dir() / "path-demo.txt"scratch.touch()println scratch.exists() # trueprintln scratch.is_file() # true
fio::write(scratch.to_string(), "hello\nworld")println scratch.readlines() # ["hello", "world"]
scratch.remove()println scratch.exists() # false| Method | Does |
|---|---|
exists(), is_file(), is_dir() | Test the path. |
read(), readlines(), readbytes() | Read the file. |
touch(), mkdir() | Create a file, or a directory with its parents. |
copy(dest) | Copy the file. |
remove(), rmdir() | Delete a file or directory. |
abspath() | The absolute path as a string. |
parents() | Every ancestor directory, nearest first. |
find(part) | The nearest ancestor (or self) whose name is part. |
root() | The root of the path. |
Path.cwd(), Path.temp_dir(), Path.temp_file() | The working directory, temp directory, or a new temp file. |
For writing files, use fio with path.to_string().
Path struct
Section titled “Path struct”Inherits from Hashable.
A struct representing a filesystem path.
Path.new
Section titled “Path.new”Path.new(path: string)Creates a new Path instance.
| Parameter | Type | Default | Description |
|---|---|---|---|
path | string | The path as a string. |
Path.cwd
Section titled “Path.cwd”static Path.cwd(): PathReturns the absolute path to the current working directory.
Returns Path — Returns a path representing the absolute path of the current working directory.
Path.path_string
Section titled “Path.path_string”static Path.path_string(new_path): stringCoerces a path into a string.
| Parameter | Type | Default | Description |
|---|---|---|---|
new_path | any | A new path (or path part) either as a string or a Path. |
Returns string — Returns a string representing a path.
Throws TypeError — Expects new_path to be a string or a Path.
Path.temp_dir
Section titled “Path.temp_dir”static Path.temp_dir(): PathReturns a path to the temp directory.
Returns Path — Returns a path to the temp directory.
Path.temp_file
Section titled “Path.temp_file”static Path.temp_file(): PathCreates a 0 byte file and returns its path.
Returns Path — Returns a path to the file.
.to_string
Section titled “.to_string”to_string(): stringOverrides the to_string() method to return the path as a string.
Returns string — Returns a string representing the path.
operator /
Section titled “operator /”fn /(new_path): PathOperator overload to combine (join) paths.
| Parameter | Type | Default | Description |
|---|---|---|---|
new_path | any | A new path (or path part) either as a string or a Path. |
Returns Path — Returns a path containing this path and the new path.
Throws TypeError — Expects new_path to be a string or a Path.
.abspath
Section titled “.abspath”abspath(): stringReturns the absolute path as a string.
Returns string — A string containing the absolute path.
.combine
Section titled “.combine”combine(new_path): PathCombines (joins) this path with another.
| Parameter | Type | Default | Description |
|---|---|---|---|
new_path | any | A new path (or path part) either as a string or a Path. |
Returns Path — Returns a path containing this path and the new path.
Throws TypeError — Expects new_path to be a string or a Path.
.contains
Section titled “.contains”contains(path_part: string): booleanChecks if the parts contains a given part.
| Parameter | Type | Default | Description |
|---|---|---|---|
path_part | string | The path part as a string. |
Returns boolean — Returns true when the path contains the given path part.
.mkdir
Section titled “.mkdir”mkdir(): booleanCreates a directory from the path.
Returns boolean — Returns true when the directory is created successfully.
copy(new_path): booleanCopies data from one path to another. If the path is a directory, all subdirectory contents are copied as well.
| Parameter | Type | Default | Description |
|---|---|---|---|
new_path | any | A new path either as a string or a Path. |
Returns boolean — Returns true when the data was copied.
Throws TypeError — Expects new_path to be a string or a Path.
.exists
Section titled “.exists”exists(): booleanChecks if the path exists.
Returns boolean — Returns true when the path exists on the filesystem.
ext(): stringGets the file extension of the path.
Returns string — Returns a string containing the file extension of the path.
find(path_part: string): PathGets a new path from a path part.
| Parameter | Type | Default | Description |
|---|---|---|---|
path_part | string | A part of a path as a string. |
Returns Path — Returns the path at the given path part.
.is_dir
Section titled “.is_dir”is_dir(): booleanChecks if the path is a directory.
Returns boolean — Returns true when the path is a directory.
.is_file
Section titled “.is_file”is_file(): booleanChecks if the path is a file.
Returns boolean — Returns true when the path is a file.
.joinpath
Section titled “.joinpath”joinpath(path_parts: list): PathCombines (joins) multiple path parts to this path.
| Parameter | Type | Default | Description |
|---|---|---|---|
path_parts | list | A list of path parts. |
Returns Path — Returns a new path by combining this path with a list of path parts.
name(): stringGets the last part of the path as a string.
Returns string — Returns a string containing the last part of the path.
.parent
Section titled “.parent”parent(): PathGets the parent path.
Returns Path — Returns a Path containing the parent of this path.
.parents
Section titled “.parents”parents(): listGets a list of paths containing all parents of this path.
Returns list — Returns a list of paths containing all parents of this path.
.parts
Section titled “.parts”parts(): listGets the list of path parts in this path.
Returns list — Returns a list of path parts in this path.
.remove
Section titled “.remove”remove(): booleanRemoves this path from the file system.
Returns boolean — Returns true when the path was successfully removed from the file system.
Throws FileSystemError — Throws when the path is a non-empty directory. Use rmdir() instead.
.rmdir
Section titled “.rmdir”rmdir(): booleanRemoves the path from the file system, and all of its subpaths.
Returns boolean — Returns true when the path was successfully removed from the file system.
root(): PathGets the path root.
Returns Path — Returns a Path representing the root of this path.
.readbytes
Section titled “.readbytes”readbytes(): bytesGets all content from the path as bytes.
Returns bytes — Returns bytes containing the content of the file this path points to.
.readlines
Section titled “.readlines”readlines(): listGets all content from the path as a list of strings representing the lines in the file.
Returns list — Returns a list of stirngs containing the line content of the file this path points to.
read(): stringGets all content from the path as a string.
Returns bytes — Returns bytes containing the content of the file this path points to.
.read_bytes
Section titled “.read_bytes”read_bytes(): bytesGets all content from the path as bytes.
Returns bytes — Returns bytes containing the content of the file this path points to.
.read_lines
Section titled “.read_lines”read_lines(): listGets all content from the path as a list of strings representing the lines in the file.
Returns list — Returns a list of stirngs containing the line content of the file this path points to.
.touch
Section titled “.touch”touch(): booleanCreates a file from the path.
Returns boolean — Returns true when the file is created successfully.