Path joining and parts
Path(*pathsegments)
PurePath.joinpath(*pathsegments)
PurePath.with_name(name)
PurePath.with_suffix(suffix)Build and inspect a path without reading the filesystem.
- Path is a concrete path and can perform I/O. PurePosixPath and PureWindowsPath are the corresponding pure classes.
- Use as_posix() when you need a stable string in examples or tests. str(path) uses the current operating system separator.
from pathlib import Path
path = Path("src") / "app" / "main.py"
print(path.as_posix())
print(path.name, path.stem, path.suffix)
print(path.parts)
print(path.with_name("test.py").as_posix())
print(path.with_suffix(".md").as_posix())Expected output
src/app/main.py
main.py main .py
('src', 'app', 'main.py')
src/app/test.py
src/app/main.md