How can I get the path of a file without the file basename?
Something like /a/path/to/my/file.txt
--> /a/path/to/my/
Tried to use .split('/')
but it's not clean, since you have to add the final /
, and it's not compatible with all OSes.
You can import os
>>> filepath
'/a/path/to/my/file.txt'
>>> os.path.dirname(filepath)
'/a/path/to/my'
>>>
(dirname, filename) = os.path.split(path)
Since Python 3.4 you can use Pathlib.
from pathlib import Path
path = Path("/a/path/to/my/file.txt")
print(path.parent)