Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Full splitting of a file's pathname

Reply
Thread Tools

Full splitting of a file's pathname

 
 
tac-tics
Guest
Posts: n/a
 
      07-10-2006
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.

 
Reply With Quote
 
 
 
 
BartlebyScrivener
Guest
Posts: n/a
 
      07-10-2006
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

 
Reply With Quote
 
 
 
 
Iain King
Guest
Posts: n/a
 
      07-10-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

 
Reply With Quote
 
Simon Forman
Guest
Posts: n/a
 
      07-11-2006
BartlebyScrivener wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FileUpLoad Control - Retrieving full pathname marathoner ASP .Net Web Controls 3 03-22-2007 03:18 AM
FileUpLoad Control - retrieving full pathname marathoner ASP .Net 1 03-14-2007 08:25 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments