views:

67

answers:

1

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

+5  A: 

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.

Daniel Kluev
i knew there had to be a better way to do it. thanks, still learning python
Brock Woolf