Full splitting of a file's pathname

Discussion in 'Python' started by tac-tics, Jul 10, 2006.

  1. tac-tics

    tac-tics Guest

    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.
     
    tac-tics, Jul 10, 2006
    #1
    1. Advertising

  2. 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
     
    BartlebyScrivener, Jul 10, 2006
    #2
    1. Advertising

  3. tac-tics

    Iain King Guest

    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
     
    Iain King, Jul 10, 2006
    #3
  4. tac-tics

    Simon Forman Guest

    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
     
    Simon Forman, Jul 11, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Ericson
    Replies:
    0
    Views:
    428
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    442
  3. John Dibling
    Replies:
    0
    Views:
    417
    John Dibling
    Jul 19, 2003
  4. marathoner
    Replies:
    1
    Views:
    412
    Alexey Smirnov
    Mar 14, 2007
  5. marathoner

    FileUpLoad Control - Retrieving full pathname

    marathoner, Mar 14, 2007, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    113
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 22, 2007
Loading...

Share This Page