Remove Last Path Component In a String

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.


You can also use os.path.split, like this

>>> import os
>>> os.path.split('product/bin/client')
('product/bin', 'client')

It splits the path into two parts and returns them in a tuple. You can assign the values in variables and then use them, like this

>>> head, tail = os.path.split('product/bin/client')
>>> head
'product/bin'
>>> tail
'client'