FAQ
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.

Search Discussions

  • BartlebyScrivener at Jul 10, 2006 at 2:52 pm
    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
  • Kerry, Richard at Jul 10, 2006 at 2:59 pm
    Unless it's the sort of operating system that uses something like :

    Sys$disk:[foo.bar.moo.lar]myfile.txt


    (VaxVMS, from quite a few years ago)
    And if I recall, the parsing function provided would also separate the
    extension from the main part of the file's name.


    Unhelpfully,
    Richard.



    -----Original Message-----
    From: python-list-bounces+richard.kerry=siemens.com at python.org
    [mailto:python-list-bounces+richard.kerry=siemens.com at python.org] On
    Behalf Of BartlebyScrivener
    Sent: 10 July 2006 15:52
    To: python-list at python.org
    Subject: Re: Full splitting of a file's pathname

    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
  • Simon Forman at Jul 11, 2006 at 1:04 am

    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
  • Iain King at Jul 10, 2006 at 3:25 pm

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 10, '06 at 2:28p
activeJul 11, '06 at 1:04a
posts5
users5
websitepython.org

People

Translate

site design / logo © 2021 Grokbase