Full splitting of a file's pathname

Iain King iainking at gmail.com
Mon Jul 10 11:25:03 EDT 2006


tac-tics wrote:
> I know about os.path.split(), but Is there any standard function for
> "fully" splitting a file's pathname? A function that is the opposite of
> the os.path.join() function? For example:
>
> >>> ret = myster_function(./foo/bar/moo/lar/myfile.txt)
> >>> print ret
> ['.', 'foo', 'bar', 'moo', 'lar', 'myfile.txt']
>
> In the meanwhile, I'll do this by hand. I'm just curious if there is a
> standard way to do this.

Simple function using os.path.split (so it should be fairly
compatible):

def split(path):
    h,t = os.path.split(path)
    if h == path:
        return [h]
    else:
        return split(h) + [t]

You could throw in os.path.splitdrive and os.path.splitunc, if you
wanted to be really complete.

Iain




More information about the Python-list mailing list