Skip to main content
Log in

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 HOMEonposixorHOME 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 HOMEonposixorHOME 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: 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?