Fspath Sample
Fri 14 November 2025
import os
class MyPath:
def __init__(self, path):
self.path = path
def __fspath__(self):
return self.path
p = MyPath("example.txt")
# Python will call __fspath__ internally
print(os.fspath(p)) # Output: example.txt
# You can now use it directly with file operations
# with open(p, "w") as f:
# f.write("Hello!")
example.txt
# Read the content
with open(p, "r") as f:
content = f.read()
print(content)
one
two
Score: 5
Category: dunder_methods