Posted By: Anonymous
Could someone tell me how to get the parent directory of a path in Python in a cross platform way. E.g.
C:Program Files ---> C:
C: ---> C:
If the directory doesn’t have a parent directory, it returns the directory itself. The question might seem simple but I couldn’t dig it up through Google.
Update from Python 3.4
from pathlib import Path path = Path("/here/your/path/file.txt") print(path.parent)
import os print os.path.abspath(os.path.join(yourpath, os.pardir))
yourpath is the path you want the parent for.