Full splitting of a file's pathname

T

tac-tics

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:
['.', '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.
 
B

BartlebyScrivener

I don't know if it's "standard," but why not just:

dir = './foo/bar/moo/lar/myfile.txt'
dir.split('/')

['.', 'foo', 'bar', 'moo', 'lar', 'myfile.txt']

rd
 
I

Iain King

tac-tics said:
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:
['.', '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
 
S

Simon Forman

BartlebyScrivener said:
I don't know if it's "standard," but why not just:

dir = './foo/bar/moo/lar/myfile.txt'
dir.split('/')

['.', 'foo', 'bar', 'moo', 'lar', 'myfile.txt']

rd

There's also os.path.sep, from the docs: "The character used by the
operating system to separate pathname components."
But a recursive function like the one Iain King posted is likely the
best way to go.

~Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top