os.path.join, os.path.split, os.path.basename, os.path.splitext
os.path.join(path, *paths)
os.path.split(path)
os.path.basename(path)
os.path.splitext(path)Join path segments and split a path into directory, file name, or suffix. These helpers operate on strings and do not open files.
| Parameter | Description |
|---|---|
path, paths | Path segments or a path string. join inserts the current operating system separator. |
Returns: join returns a single path string. split returns (head, tail). basename returns the final component. splitext returns (root, ext), where ext includes the leading dot or is empty.
- The slash-separated results were measured on POSIX (macOS). They are not Windows output.
- The list of names from listdir is covered in the next section because it requires a real directory.
import os
print(os.path.join("home", "ada", "src.py"))
print(os.path.split(os.path.join("sub", "b.txt")))
print(os.path.basename("sub/b.txt"), os.path.splitext("b.txt"))Expected output
home/ada/src.py
('sub', 'b.txt')
b.txt ('b', '.txt')Related: pathlib.Path