47. Path Manipulation with os Module
1. Joining Paths with os.path.join()
import os
path = os.path.join('folder', 'subfolder', 'file.txt')
print(f"Full path: {path}")2. Getting the Absolute Path with os.path.abspath()
import os
relative_path = 'folder/file.txt'
absolute_path = os.path.abspath(relative_path)
print(f"Absolute path: {absolute_path}")3. Checking if a Path Exists with os.path.exists()
4. Checking if a Path is a File with os.path.isfile()
5. Checking if a Path is a Directory with os.path.isdir()
6. Getting the Directory Name with os.path.dirname()
7. Getting the Base Name of a Path with os.path.basename()
8. Splitting the Path into Directory and File Name with os.path.split()
9. Normalizing a Path with os.path.normpath()
10. Getting the File Extension with os.path.splitext()
Last updated