Special Method Protocols

__fspath__() — path protocol

Provide filesystem path representation to os.fspath() and path-aware APIs. Return str or bytes, not a Path object. The method does not open or validate a filesystem path.

Formal Syntax & Structure

python syntaxpython
def __fspath__(self):

Return Value

str | bytes

Filesystem path representation.

Working Example

Example Snippetpython
import osclass Report:    def __fspath__(self): return "report.txt"print(os.fspath(Report()))
Python Reference