I have a path:
myPath = "C:\Users\myFile.txt"
I would like to remove the end path so that the string only contains:
"C:\Users"
So far I am using split, but it just gives me a list, and im stuck at this point.
myPath = myPath.split(os.sep)
Thanks
I have a path:
myPath = "C:\Users\myFile.txt"
I would like to remove the end path so that the string only contains:
"C:\Users"
So far I am using split, but it just gives me a list, and im stuck at this point.
myPath = myPath.split(os.sep)
Thanks
You should not manipulate paths directly, there is os.path module for that.
>>> import os.path
>>> print os.path.dirname("C:\Users\myFile.txt")
C:\Users
>>> print os.path.dirname(os.path.dirname("C:\Users\myFile.txt"))
C:\
Like this.