Mojo struct
Path
struct Path
The Path object.
Fields
- path (
String
): The underlying path string representation.
Implemented traits
AnyType
,
Boolable
,
CollectionElement
,
CollectionElementNew
,
Copyable
,
EqualityComparable
,
ExplicitlyCopyable
,
Hashable
,
KeyElement
,
Movable
,
PathLike
,
Stringable
,
UnknownDestructibility
,
Writable
Methods
__init__
__init__(out self)
Initializes a path with the current directory.
@implicit
__init__(out self, path: String)
Initializes a path with the provided path.
Args:
- path (
String
): The file system path.
__init__(out self, *, other: Self)
Copy the object.
Args:
- other (
Self
): The value to copy.
__bool__
__bool__(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, 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, 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, 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, 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, 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__(mut 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) -> String
Returns a string representation of the path.
Returns:
A string representation of the path.
write_to
write_to[W: Writer](self, mut writer: W)
Formats this path to the provided Writer.
Parameters:
- W (
Writer
): A type conforming to the Writable trait.
Args:
- writer (
W
): The object to write to.
__fspath__
__fspath__(self) -> String
Returns a string representation of the path.
Returns:
A string representation of the path.
__repr__
__repr__(self) -> String
Returns a printable representation of the path.
Returns:
A printable representation of the path.
__hash__
__hash__(self) -> UInt
Hash the underlying path string using builtin hash.
Returns:
An integer value containing the hash of the path string.
__hash__[H: _Hasher](self, mut hasher: H)
Updates hasher with the path string value.
Parameters:
- H (
_Hasher
): The hasher type.
Args:
- hasher (
H
): The hasher instance.
stat
stat(self) -> stat_result
Returns the stat information on the path.
Returns:
A stat_result object containing information about the path.
lstat
lstat(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) -> 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
Expands a prefixed ~
with $HOME
on posix or $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 $HOME
on posix or $USERPROFILE
on windows. If environment variables are not set it returns ~
.
Returns:
Path to user home directory.
is_dir
is_dir(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) -> 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) -> String
Returns content of the file.
Returns:
Contents of file as string.
read_bytes
read_bytes(self) -> List[SIMD[uint8, 1]]
Returns content of the file as bytes.
Returns:
Contents of file as list of bytes.
write_text
write_text[stringable: Stringable](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) -> 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, *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) -> List[Path]
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!