Mojo struct
Path
The Path object.
Fieldsβ
- βpath (
String
): The underlying path string representation.
Implemented traitsβ
AnyType
,
Boolable
,
CollectionElement
,
CollectionElementNew
,
Copyable
,
EqualityComparable
,
ExplicitlyCopyable
,
Formattable
,
Hashable
,
KeyElement
,
Movable
,
PathLike
,
Stringable
Methodsβ
__init__
β
__init__(inout self: Self)
Initializes a path with the current directory.
__init__(inout self: Self, path: String)
Initializes a path with the provided path.
Args:
- βpath (
String
): The file system path.
__init__(inout self: Self, *, other: Self)
Copy the object.
Args:
- βother (
Self
): The value to copy.
__copyinit__
β
__copyinit__(inout self: Self, existing: Self)
Copy constructor for the path struct.
Args:
- βexisting (
Self
): The existing struct to copy from.
__moveinit__
β
__moveinit__(inout self: Self, owned existing: Self)
Move data of an existing Path into a new one.
Args:
- βexisting (
Self
): The existing Path.
__bool__
β
__bool__(self: Self) -> Bool
Checks if the path is not empty.
Returns:
True if the path length is greater than zero, and False otherwise.
__eq__
β
__eq__(self: Self, other: Self) -> Bool
Returns True if the two paths are equal.
Args:
- βother (
Self
): The other path to compare against.
Returns:
True if the paths are equal and False otherwise.
__eq__(self: Self, other: String) -> Bool
Returns True if the two paths are equal.
Args:
- βother (
String
): The other path to compare against.
Returns:
True if the String and Path are equal, and False otherwise.
__ne__
β
__ne__(self: Self, other: Self) -> Bool
Returns True if the two paths are not equal.
Args:
- βother (
Self
): The other path to compare against.
Returns:
True if the paths are not equal and False otherwise.
__truediv__
β
__truediv__(self: Self, suffix: Self) -> Self
Joins two paths using the system-defined path separator.
Args:
- βsuffix (
Self
): The suffix to append to the path.
Returns:
A new path with the suffix appended to the current path.
__truediv__(self: Self, suffix: String) -> Self
Joins two paths using the system-defined path separator.
Args:
- βsuffix (
String
): The suffix to append to the path.
Returns:
A new path with the suffix appended to the current path.
__itruediv__
β
__itruediv__(inout self: Self, suffix: String)
Joins two paths using the system-defined path separator.
Args:
- βsuffix (
String
): The suffix to append to the path.
__str__
β
__str__(self: Self) -> String
Returns a string representation of the path.
Returns:
A string representation of the path.
format_to
β
format_to(self: Self, inout writer: Formatter)
Formats this path to the provided formatter.
Args:
- βwriter (
Formatter
): The formatter to write to.
__fspath__
β
__fspath__(self: Self) -> String
Returns a string representation of the path.
Returns:
A string representation of the path.
__repr__
β
__repr__(self: Self) -> String
Returns a printable representation of the path.
Returns:
A printable representation of the path.
__hash__
β
__hash__(self: Self) -> UInt
Hash the underlying path string using builtin hash.
Returns:
An integer value containing the hash of the path string.
stat
β
stat(self: Self) -> stat_result
Returns the stat information on the path.
Returns:
A stat_result object containing information about the path.
lstat
β
lstat(self: Self) -> stat_result
Returns the lstat information on the path. This is similar to stat, but if the file is a symlink then it gives you information about the symlink rather than the target.
Returns:
A stat_result object containing information about the path.
exists
β
exists(self: Self) -> Bool
Returns True if the path exists and False otherwise.
Returns:
True if the path exists on disk and False otherwise.
expanduser
β
expanduser(self: Self) -> Self
Expands a prefixed ~
with USERPROFILE on windows. If environment variables are not set or the path
is not prefixed with ~
, returns the path
unmodified.
Returns:
The expanded path.
home
β
static home() -> Self
Returns USERPROFILE on windows. If environment variables are not set it returns ~
.
Returns:
Path to user home directory.
is_dir
β
is_dir(self: Self) -> Bool
Returns True if the path is a directory and False otherwise.
Returns:
Return True if the path points to a directory (or a link pointing to a directory).
is_file
β
is_file(self: Self) -> Bool
Returns True if the path is a file and False otherwise.
Returns:
Return True if the path points to a file (or a link pointing to a file).
read_text
β
read_text(self: Self) -> String
Returns content of the file.
Returns:
Contents of file as string.
read_bytes
β
read_bytes(self: Self) -> List[SIMD[uint8, 1], 0]
Returns content of the file as bytes.
Returns:
Contents of file as list of bytes.
write_text
β
write_text[stringable: Stringable](self: Self, value: stringable)
Writes the value to the file as text.
Parameters:
- βstringable (
Stringable
): The Stringable type.
Args:
- βvalue (
stringable
): The value to write.
suffix
β
suffix(self: Self) -> String
The path's extension, if any. This includes the leading period. For example: '.txt'. If no extension is found, returns the empty string.
Returns:
The path's extension.
joinpath
β
joinpath(self: Self, *pathsegments: String) -> Self
Joins the Path using the pathsegments.
Args:
- β*pathsegments (
String
): The path segments.
Returns:
The path concatenation with the pathsegments using the directory separator.
listdir
β
listdir(self: Self) -> List[Path, 0]
Gets the list of entries contained in the path provided.
Returns:
The list of entries in the path provided.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
π What went wrong?