How can I extract the folder path from file path in Python?

I would like to get just the folder path from the full path to a file.

For example T:\Data\DBDesign\DBDesign_93_v141b.mdb and I would like to get just T:\Data\DBDesign (excluding the \DBDesign_93_v141b.mdb).

I have tried something like this:

    existGDBPath = r'T:\Data\DBDesign\DBDesign_93_v141b.mdb'
    wkspFldr = str(existGDBPath.split('\\')[0:-1])
    print wkspFldr

but it gave me a result like this:

    ['T:', 'Data', 'DBDesign']

which is not the result that I require (being T:\Data\DBDesign).

Any ideas on how I can get the the path to my file?

You were almost there with your use of the split function. You just needed to join the strings, like follows.

    >>> '\\'.join(existGDBPath.split('\\')[0:-1])

Although, I would recommend using the os.path.dirname function to do this, you just need to pass the string, and it'll do the work for you. Since, you seem to be on windows, consider using the abspath function too. An example -

    >>> os.path.dirname(os.path.abspath(existGDBPath))

If you want both the file name and the directory path after being split, you can use the os.path.split function which returns a tuple, as follows.

    >>> os.path.split(os.path.abspath(existGDBPath))
    ('T:\\Data\\DBDesign', 'DBDesign_93_v141b.mdb')