How to get the folder from a path in Python

Getting the folder from a path returns the path of the parent directory. For example, getting the folder from the path “/path/to/file/sample.txt” returns “/path/to/file”.

Solution for How to get the folder from a path in Python : You can use pathlib.PurePath.parent to get the folder from a path Call pathlib.PurePath(path) to return a pathlib.PurePath object from a path. Use PurePath.parent with PurePath as the result of the previous step to return the path of the folder.


how-to-get-the-folder-from-a-path-in-python