Path class
Path class
Section titled “Path class”Concrete path that layers filesystem I/O on top of PurePath semantics.
Signature:
export declare class Path extends PurePathExtends: PurePath
Remarks
Section titled “Remarks”Mirrors CPython’s `pathlib.Path`. The runtime selects PosixPath or WindowsPath during construction so the instance can call into Node’s filesystem APIs safely. Each I/O method provides an asynchronous default that resolves to a promise, alongside a synchronous companion suffixed with Sync, preserving ergonomic parity with the reference implementation while embracing JavaScript’s async-first patterns.
Path also introduces policy-driven behaviour for relative computations to accommodate module-resolution workflows common in the JS ecosystem. The lexical defaults remain faithful to CPython.
Example
Section titled “Example”Reading text from a sibling file
import { Path } from "pathlib-ts";
const readme = new Path("./README.md");const contents = await readme.readText();
console.log(contents.slice(0, 40));Properties
Section titled “Properties”|
Property |
Modifiers |
Type |
Description |
|---|---|---|---|
|
|
Provides cached stat-like information gathered during directory iteration. | ||
|
|
(Optional) | ||
|
Determines whether this path can be expressed relative to another according to the selected policy. | |||
|
Return the relative path to another path identified by the passed arguments. If the operation is not possible (because this is not related to the other path), raise ValueError. |
Methods
Section titled “Methods”|
Method |
Modifiers |
Description |
|---|---|---|
|
Return an absolute version of this path without resolving symlinks. | ||
|
Return an absolute path without resolving symlinks, synchronously. | ||
|
Recursively copy this file or directory tree to the destination. | ||
|
Recursively copy the file or directory tree to the destination synchronously. | ||
|
|
Return a new path pointing to the current working directory. | |
|
|
Create a Path pointing to the current working directory synchronously. | |
|
Resolves to | ||
|
Checks for path existence synchronously. | ||
|
Return a new path with expanded | ||
|
Expand leading | ||
|
Globs files within this subtree asynchronously. Iterate over this subtree and yields all existing files (of any kind, including directories) matching the given relative pattern. | ||
|
Performs a blocking glob search relative to this subtree and yield all existing files (of any kind, including directories) matching the given relative pattern. | ||
|
|
Return a new path pointing to the user’s home directory. | |
|
|
Return the user’s home directory synchronously. | |
|
Resolves to | ||
|
Tests whether the path is a directory synchronously. | ||
|
Resolves to | ||
|
Tests whether the path is a regular file synchronously. | ||
|
Resolves to | ||
|
Determines synchronously whether the path points to a symbolic link. | ||
|
Resolves directory entries asynchronously as Path instances or native Dirents. | ||
|
Streams directory entries lazily using async iteration without materialising the entire directory listing. | ||
|
Streams directory entries synchronously using generators. | ||
|
Enumerates directory contents synchronously. | ||
|
Asynchronously retrieves metadata about a symlink rather than its target. Like Path.stat(), except if the path points to a symlink, the symlink’s status information is returned, rather than its target’s. | ||
|
Retrieves symlink metadata synchronously. | ||
|
Create a directory with optional | ||
|
Create a directory synchronously with optional | ||
|
Open the file with the given mode and return a Node.js stream-like handle. | ||
|
Open the file with the given mode using the synchronous helper. | ||
|
Read the file as raw bytes and return a promise that resolves to the binary contents of the pointed-to file as a bytes object. | ||
|
Read the file as raw bytes. | ||
|
Return the path to which the symbolic link points. | ||
|
Read the target of a symbolic link synchronously. | ||
|
Read the file as text using the provided encoding and return a promise that resolves to the decoded contents of pointed-to file as a string. | ||
|
Read the file as text using the provided encoding. | ||
|
Rename this path to the given target and return the new path instance. | ||
|
Rename the file or directory to a new target synchronously. | ||
|
Replace the target path, overwriting it if necessary, and return the new Path. | ||
|
Replace the file or directory synchronously, overwriting the destination. | ||
|
Make the path absolute, resolving symlinks and normalizing segments. | ||
|
Resolve the path synchronously, following symlinks and normalizing. | ||
|
Asynchronously performs a recursive glob in this subtree, and yields all existing files (of any kind, including directories) matching the given relative pattern. | ||
|
Performs a recursive glob search synchronously. | ||
|
Remove this directory; the directory must already be empty. | ||
|
Remove the directory synchronously. | ||
|
Returns filesystem metadata asynchronously. | ||
|
Retrieves filesystem metadata synchronously. | ||
|
Create the file or update its timestamps with optional mode overrides. | ||
|
Create the file or update its timestamps synchronously. | ||
|
Remove this file or link. Use Path.rmdir() for directories. | ||
|
Remove the file or link synchronously. | ||
|
Walk the directory tree from this directory. | ||
|
Traverse the directory tree synchronously and return the walk tuples. | ||
|
Asynchronously write raw bytes to the file. An existing file of the same name is overwritten. | ||
|
Write raw bytes to the file. An existing file of the same name is overwritten. | ||
|
Asynchronously write the given text to the file using the provided encoding. An existing file of the same name is overwritten. | ||
|
Write the given text to the file using the provided encoding. An existing file of the same name is overwritten. |