257

I have a string variable which represents a dos path e.g:

var = "d:\stuff\morestuff\furtherdown\THEFILE.txt"

I want to split this string into:

[ "d", "stuff", "morestuff", "furtherdown", "THEFILE.txt" ]

I have tried using split() and replace() but they either only process the first backslash or they insert hex numbers into the string.

I need to convert this string variable into a raw string somehow so that I can parse it.

What's the best way to do this?

I should also add that the contents of var i.e. the path that I'm trying to parse, is actually the return value of a command line query. It's not path data that I generate myself. Its stored in a file, and the command line tool is not going to escape the backslashes.

2
  • 8
    As you review these answers, remember that os.path.split is not working for you because you aren't escaping that string properly.
    – Jed Smith
    Jul 2, 2010 at 19:30
  • You need to escape the string or use rawstring: r"d:\stuff\morestuff\furtherdown\THEFILE.txt" to prevent things like \s being misinterpreted.
    – smci
    Apr 2, 2020 at 18:30

23 Answers 23

462

I would do

import os
path = os.path.normpath(path)
path.split(os.sep)

First normalize the path string into a proper string for the OS. Then os.sep must be safe to use as a delimiter in string function split.

7
  • 48
    As a one-liner, os.path.normpath(a_path).split(os.path.sep) Mar 31, 2016 at 17:15
  • 4
    This doesn't seem to work for path = root. In that case, the result of path.split is ['','']. In fact in general, this split() solution gives a leftmost directory with empty-string name (which could be replaced by the appropriate slash). The core problem is that a single slash (forward or backward depending on the OS) is the name of the root directory, whereas elsewhere in the path it is a separator.
    – gwideman
    Jul 6, 2016 at 2:51
  • 2
    Will it work better with an lstrip then? os.path.normpath(path).lstrip(os.path.sep).split(os.path.sep)
    – Vidar
    Jul 10, 2017 at 22:52
  • 1
    @user60561 That's because on Linux, backslash is an allowed character in filenames, whereas on Windows a forward slash isn't. That's why on Windows, normpath will recognize forward slash as a separator. On Linux, normpath will simply assume that you have a directory called \1\2 and a file or directory inside it called 3. Apr 30, 2018 at 22:03
  • 1
    This actually answers the question, thank you! The accepted answer does not consider cases where one has to fiddle with the folder-structure. Since I had to search a bit, I want to note that (for path.split(os.sep)) os.path.join(*(folder_list)) gets you back the path.
    – BadAtLaTeX
    Sep 14, 2018 at 11:48
227

I've been bitten loads of times by people writing their own path fiddling functions and getting it wrong. Spaces, slashes, backslashes, colons -- the possibilities for confusion are not endless, but mistakes are easily made anyway. So I'm a stickler for the use of os.path, and recommend it on that basis.

(However, the path to virtue is not the one most easily taken, and many people when finding this are tempted to take a slippery path straight to damnation. They won't realise until one day everything falls to pieces, and they -- or, more likely, somebody else -- has to work out why everything has gone wrong, and it turns out somebody made a filename that mixes slashes and backslashes -- and some person suggests that the answer is "not to do that". Don't be any of these people. Except for the one who mixed up slashes and backslashes -- you could be them if you like.)

You can get the drive and path+file like this:

drive, path_and_file = os.path.splitdrive(path)

Get the path and the file:

path, file = os.path.split(path_and_file)

Getting the individual folder names is not especially convenient, but it is the sort of honest middling discomfort that heightens the pleasure of later finding something that actually works well:

folders = []
while 1:
    path, folder = os.path.split(path)

    if folder != "":
        folders.append(folder)
    else:
        if path != "":
            folders.append(path)

        break

folders.reverse()

(This pops a "\" at the start of folders if the path was originally absolute. You could lose a bit of code if you didn't want that.)

11
  • @brone - I prefer to use this solution, than having to worry about escaping the backslash. thanks!
    – BeeBand
    Jul 13, 2010 at 14:43
  • 1
    I'd be happy to be proved wrong but it seems to me suggested solution does not work if a path such as this "C:\usr\rs0\my0\in111102.log" is used (unless the initial input is a raw string) ? Nov 3, 2011 at 0:55
  • 1
    It looks like this will not properly split a path if it only contains a directory in OSX such as "/path/to/my/folder/", in order to achieve that you'd want to add these two lines to the beginning: if path.endswith("/"): and path = path[:-1]. Feb 11, 2013 at 19:34
  • 1
    I prefer solution by @Tompa
    – jaycode
    Nov 26, 2015 at 6:13
  • 1
    I concur with jaycode: Tompa's solution is the canonical approach and should have been the accepted answer. This overly complex, inefficient, and error-prone alternative fails to pass muster on production code. There's no reasonable reason to attempt (...and fail, of course) to iteratively parse apart pathnames when simple string splitting succeeds with only a single line of code. Dec 11, 2015 at 4:18
143

In Python >=3.4 this has become much simpler. You can now use pathlib.Path.parts to get all the parts of a path.

Example:

>>> from pathlib import Path
>>> Path('C:/path/to/file.txt').parts
('C:\\', 'path', 'to', 'file.txt')
>>> Path(r'C:\path\to\file.txt').parts
('C:\\', 'path', 'to', 'file.txt')

On a Windows install of Python 3 this will assume that you are working with Windows paths, and on *nix it will assume that you are working with posix paths. This is usually what you want, but if it isn't you can use the classes pathlib.PurePosixPath or pathlib.PureWindowsPath as needed:

>>> from pathlib import PurePosixPath, PureWindowsPath
>>> PurePosixPath('/path/to/file.txt').parts
('/', 'path', 'to', 'file.txt')
>>> PureWindowsPath(r'C:\path\to\file.txt').parts
('C:\\', 'path', 'to', 'file.txt')
>>> PureWindowsPath(r'\\host\share\path\to\file.txt').parts
('\\\\host\\share\\', 'path', 'to', 'file.txt')

Edit: There is also a backport to python 2 available: pathlib2

0
92

You can simply use the most Pythonic approach (IMHO):

import os

your_path = r"d:\stuff\morestuff\furtherdown\THEFILE.txt"
path_list = your_path.split(os.sep)
print path_list

Which will give you:

['d:', 'stuff', 'morestuff', 'furtherdown', 'THEFILE.txt']

The clue here is to use os.sep instead of '\\' or '/', as this makes it system independent.

To remove colon from the drive letter (although I don't see any reason why you would want to do that), you can write:

path_list[0] = path_list[0][0]
2
  • 27
    This works some times. Other times (on windows at least) you will find paths that look like folder\folder2\folder3/file.txt. Its better to first normalize (os.path.normpath) the path and then split that.
    – vikki
    Jul 27, 2014 at 17:58
  • 8
    This answer was almost there. As vikki suggests, the failure to normalize pathnames before string splitting spells doom on commonplace edge-cases (e.g., /foo//bar). See Tompa's answer for a more robust solution. Dec 11, 2015 at 5:14
12

For a somewhat more concise solution, consider the following:

def split_path(p):
    a,b = os.path.split(p)
    return (split_path(a) if len(a) and len(b) else []) + [b]
2
  • This is my favourite solution to this problem. Very nice.
    – Will Moore
    Sep 30, 2014 at 21:27
  • 2
    This does not work if the path ends with /. Also, gives you an empty string at the beginning of the list if your path starts with /
    – Sorig
    Oct 19, 2016 at 11:15
11

The problem here starts with how you're creating the string in the first place.

a = "d:\stuff\morestuff\furtherdown\THEFILE.txt"

Done this way, Python is trying to special case these: \s, \m, \f, and \T. In your case, \f is being treated as a formfeed (0x0C) while the other backslashes are handled correctly. What you need to do is one of these:

b = "d:\\stuff\\morestuff\\furtherdown\\THEFILE.txt"      # doubled backslashes
c = r"d:\stuff\morestuff\furtherdown\THEFILE.txt"         # raw string, no doubling necessary

Then once you split either of these, you'll get the result you want.

4
  • @W. Craig Trader - thanks, but this path is not one that I generate myself - it comes back to me from another program and I have to store this data in a variable. I am not sure how to convert data stored in a variable into "raw text".
    – BeeBand
    Jul 13, 2010 at 14:29
  • There isn't such thing as a "raw text"... it's just how you represent it in the source. Either prepend r"" to the string, or pass it through .replace('\\', '/') Jul 13, 2010 at 14:37
  • @BeeBand, how are you getting the data back from the other program? Are you reading it from a file, a pipe, a socket? If so, then you don't need to do anything fancy; the only reason for doubling backslashes or using raw strings is to place string constants into Python code. On the other hand, if the other program is generating doubled-backslashes, then you'd want to clean that up before splitting your path. Jul 13, 2010 at 15:04
  • @W. Craig Trader - i'm reading it from a file, that gets written by another program. I couldn't get split() or replace() to work for some reason - I kept getting hex values. You're right though, I think I was barking up the wrong tree with the raw string idea - I think I was just using split() incorrectly. Because I tried some of these solutions using split() and they work for me now.
    – BeeBand
    Jul 13, 2010 at 20:13
5

I can't actually contribute a real answer to this one (as I came here hoping to find one myself), but to me the number of differing approaches and all the caveats mentioned is the surest indicator that Python's os.path module desperately needs this as a built-in function.

1
4

The stuff about about mypath.split("\\") would be better expressed as mypath.split(os.sep). sep is the path separator for your particular platform (e.g., \ for Windows, / for Unix, etc.), and the Python build knows which one to use. If you use sep, then your code will be platform agnostic.

2
  • 1
    Or os.path.split. You want to be careful with os.pathsep, because it's : on my version of Python in OS X (and os.path.split properly handles /).
    – Jed Smith
    Jul 2, 2010 at 19:26
  • 4
    You mean os.sep, not os.pathsep. Follow the wisdom in the os.sep docs: Note that knowing this is not sufficient to be able to parse or concatenate pathnames — use os.path.split() and os.path.join().
    – Jon-Eric
    Aug 16, 2012 at 19:57
3

The functional way, with a generator.

def split(path):
    (drive, head) = os.path.splitdrive(path)
    while (head != os.sep):
        (head, tail) = os.path.split(head)
        yield tail

In action:

>>> print([x for x in split(os.path.normpath('/path/to/filename'))])
['filename', 'to', 'path']
0
3

You can recursively os.path.split the string

import os
def parts(path):
    p,f = os.path.split(path)
    return parts(p) + [f] if f else [p]

Testing this against some path strings, and reassembling the path with os.path.join

>>> for path in [
...         r'd:\stuff\morestuff\furtherdown\THEFILE.txt',
...         '/path/to/file.txt',
...         'relative/path/to/file.txt',
...         r'C:\path\to\file.txt',
...         r'\\host\share\path\to\file.txt',
...     ]:
...     print parts(path), os.path.join(*parts(path))
... 
['d:\\', 'stuff', 'morestuff', 'furtherdown', 'THEFILE.txt'] d:\stuff\morestuff\furtherdown\THEFILE.txt
['/', 'path', 'to', 'file.txt'] /path\to\file.txt
['', 'relative', 'path', 'to', 'file.txt'] relative\path\to\file.txt
['C:\\', 'path', 'to', 'file.txt'] C:\path\to\file.txt
['\\\\', 'host', 'share', 'path', 'to', 'file.txt'] \\host\share\path\to\file.txt

The first element of the list may need to be treated differently depending on how you want to deal with drive letters, UNC paths and absolute and relative paths. Changing the last [p] to [os.path.splitdrive(p)] forces the issue by splitting the drive letter and directory root out into a tuple.

import os
def parts(path):
    p,f = os.path.split(path)
    return parts(p) + [f] if f else [os.path.splitdrive(p)]

[('d:', '\\'), 'stuff', 'morestuff', 'furtherdown', 'THEFILE.txt']
[('', '/'), 'path', 'to', 'file.txt']
[('', ''), 'relative', 'path', 'to', 'file.txt']
[('C:', '\\'), 'path', 'to', 'file.txt']
[('', '\\\\'), 'host', 'share', 'path', 'to', 'file.txt']

Edit: I have realised that this answer is very similar to that given above by user1556435. I'm leaving my answer up as the handling of the drive component of the path is different.

1

It works for me:

>>> a=r"d:\stuff\morestuff\furtherdown\THEFILE.txt"
>>> a.split("\\")
['d:', 'stuff', 'morestuff', 'furtherdown', 'THEFILE.txt']

Sure you might need to also strip out the colon from the first component, but keeping it makes it possible to re-assemble the path.

The r modifier marks the string literal as "raw"; notice how embedded backslashes are not doubled.

6
  • @unwind - the r in front of your string, what does that refer to?
    – BeeBand
    Jul 2, 2010 at 15:45
  • 2
    r means raw string - it auto-escapes \ characters. It's useful to use whenever you're doing paths. Jul 2, 2010 at 15:46
  • 1
    @BeeBand: you don't need to care; the r"" is just something that matters during compilation/parsing of the code, it's not something that becomes a property of the string once parsed. It just means "here's a string literal, but don't interpret any backslashes as having any other meaning than being backslashes".
    – unwind
    Jul 2, 2010 at 16:05
  • 3
    I think it might be helpful to mention you minus well do it more ambiguous using a.split(os.sep) instead of hard coding it? Jul 2, 2010 at 17:42
  • 5
    I have to downvote you for missing a chance to explain os.path.split and os.pathsep, considering both of those are far more portable than what you have written. It might not matter to OP now, but it will when he's writing something that needs to move platforms.
    – Jed Smith
    Jul 2, 2010 at 19:27
1

I use the following as since it uses the os.path.basename function it doesn't add any slashes to the returned list. It also works with any platform's slashes: i.e window's \\\\ or unix's /. And furthermore, it doesn't add the \\\\\\\\ that windows uses for server paths :)

def SplitPath( split_path ):
    pathSplit_lst   = []
    while os.path.basename(split_path):
        pathSplit_lst.append( os.path.basename(split_path) )
        split_path = os.path.dirname(split_path)
    pathSplit_lst.reverse()
    return pathSplit_lst

So for:

\\\\\\\server\\\\folder1\\\\folder2\\\\folder3\\\\folder4

You get:

['server','folder1','folder2','folder3','folder4']
1
  • 1
    That doesn't follow the invariant that passing your result to os.path.join() should return the original string. I'd say the correct output for your example input is [r'\\','server','folder1','folder2','folder3','folder4']. I.e. what os.path.split() does.
    – Jon-Eric
    Aug 16, 2012 at 20:08
1

really easy and simple way to do it:

var.replace('\\', '/').split('/')

1
  • 1
    It may do the work in most cases, but it is not cross-platform and can be dangerous. For example, in Linux, the `\` character is legal in file and directory names. Your code will split such files/directories, which is an undesired result Jan 1, 2023 at 11:52
0

Just like others explained - your problem stemmed from using \, which is escape character in string literal/constant. OTOH, if you had that file path string from another source (read from file, console or returned by os function) - there wouldn't have been problem splitting on '\\' or r'\'.

And just like others suggested, if you want to use \ in program literal, you have to either duplicate it \\ or the whole literal has to be prefixed by r, like so r'lite\ral' or r"lite\ral" to avoid the parser converting that \ and r to CR (carriage return) character.

There is one more way though - just don't use backslash \ pathnames in your code! Since last century Windows recognizes and works fine with pathnames which use forward slash as directory separator /! Somehow not many people know that.. but it works:

>>> var = "d:/stuff/morestuff/furtherdown/THEFILE.txt"
>>> var.split('/')
['d:', 'stuff', 'morestuff', 'furtherdown', 'THEFILE.txt']

This by the way will make your code work on Unix, Windows and Mac... because all of them do use / as directory separator... even if you don't want to use the predefined constants of module os.

4
  • Unfortunately the data is being returned to me from another program that I run from my python script. I don't have any control over whether to use '\' or '/' - it is the third party program that determines this ( probably on a platform basis ).
    – BeeBand
    Jul 13, 2010 at 14:33
  • @BeeBand: Ah, then you won't have the problem you experienced during testing, when you provided the string as literal in your program. Or you can do the following evil hack after receiving the path: var = var.replace('\\','/') - replace \ with / and proceed working with forward slashes only :)
    – Nas Banov
    Jul 13, 2010 at 20:30
  • that is indeed an evil hack :o)
    – BeeBand
    Jul 13, 2010 at 22:15
  • @BeeBand: that's why i warned. When i say something is evil, i don't necessarily mean it should never be used - but one should very much be aware why they are using it and alert of unintended consequences. In this case, a very unlikely consequence is that if this is used on Unix file system with `` use in file or directory name (it's really hard but possible) - this code will 'break'
    – Nas Banov
    Jul 14, 2010 at 0:33
0
+25

Let assume you have have a file filedata.txt with content:

d:\stuff\morestuff\furtherdown\THEFILE.txt
d:\otherstuff\something\otherfile.txt

You can read and split the file paths:

>>> for i in open("filedata.txt").readlines():
...     print i.strip().split("\\")
... 
['d:', 'stuff', 'morestuff', 'furtherdown', 'THEFILE.txt']
['d:', 'otherstuff', 'something', 'otherfile.txt']
2
  • this does indeed work, thanks! But I chose brone's solution because I prefer not to worry about escaping the backslash.
    – BeeBand
    Jul 13, 2010 at 14:44
  • 9
    Not pythonic since it is filesystem dependent.
    – jb.
    Jun 29, 2012 at 17:52
0

re.split() can help a little more then string.split()

import re    
var = "d:\stuff\morestuff\furtherdown\THEFILE.txt"
re.split( r'[\\/]', var )
['d:', 'stuff', 'morestuff', 'furtherdown', 'THEFILE.txt']

If you also want to support Linux and Mac paths, just add filter(None,result), so it will remove the unwanted '' from the split() since their paths starts with '/' or '//'. for example '//mount/...' or '/var/tmp/'

import re    
var = "/var/stuff/morestuff/furtherdown/THEFILE.txt"
result = re.split( r'[\\/]', var )
filter( None, result )
['var', 'stuff', 'morestuff', 'furtherdown', 'THEFILE.txt']
0

Below line of code can handle:

  1. C:/path/path
  2. C://path//path
  3. C:\path\path
  4. C:\path\path

path = re.split(r'[///\]', path)

0

One recursive for the fun.

Not the most elegant answer, but should work everywhere:

import os

def split_path(path):
    head = os.path.dirname(path)
    tail = os.path.basename(path)
    if head == os.path.dirname(head):
        return [tail]
    return split_path(head) + [tail]
1
  • indeed, sorry. Should have carefully read the question ... a 'dos' path.
    – DuGNu
    Jan 16, 2020 at 15:57
0

Adapted the solution of @Mike Robins avoiding empty path elements at the beginning:

def parts(path):
    p,f = os.path.split(os.path.normpath(path))
    return parts(p) + [f] if f and p else [p] if p else []

os.path.normpath() is actually required only once and could be done in a separate entry function to the recursion.

0

I'm not actually sure if this fully answers the question, but I had a fun time writing this little function that keeps a stack, sticks to os.path-based manipulations, and returns the list/stack of items.

def components(path):
    ret = []
    while len(path) > 0:
        path, crust = split(path)
        ret.insert(0, crust)
    return ret
1
  • what is split()?
    – user3064538
    Aug 24, 2021 at 6:03
0
from os import path as os_path

and then

def split_path_iter(string, lst):
    head, tail = os_path.split(string)
    if head == '':
        return [string] + lst
    else:
        return split_path_iter(head, [tail] + lst)

def split_path(string):
    return split_path_iter(string, [])

or, inspired by the above answers (more elegant):

def split_path(string):
    head, tail = os_path.split(string)
    if head == '':
        return [string]
    else:
        return split_path(head) + [tail]
1
  • This question already has a lot of answers, can you explain what new information your answer provides, or how it improves on the solutions already here?
    – joanis
    Apr 7, 2021 at 13:57
0

It is a shame! python os.path doesn't have something like os.path.splitall

anyhow, this is what works for me, credit: https://www.oreilly.com/library/view/python-cookbook/0596001673/ch04s16.html

import os

a = '/media//max/Data/'

def splitall(path):
    # https://www.oreilly.com/library/view/python-cookbook/0596001673/ch04s16.html
    allparts = []
    while 1:
        parts = os.path.split(path)
        if parts[0] == path:  # sentinel for absolute paths
            allparts.insert(0, parts[0])
            break
        elif parts[1] == path: # sentinel for relative paths
            allparts.insert(0, parts[1])
            break
        else:
            path = parts[0]
            allparts.insert(0, parts[1])
    return allparts

x = splitall(a)
print(x)

z = os.path.join(*x)
print(z)

output:

['/', 'media', 'max', 'Data', '']
/media/max/Data/
-2

use ntpath.split()

6
  • when i use os.path.split() I get, (d:\\stuff, morestuff\x0curtherdown\thefile.mux)
    – BeeBand
    Jul 2, 2010 at 15:46
  • As BeeBand pointed out, os.path.split() really doesn't do the desired thing.
    – unwind
    Jul 2, 2010 at 15:47
  • sorry I just realized os.path only works depending on your os. ntpath will parse dos paths.
    – deft_code
    Jul 2, 2010 at 15:50
  • even with ntpath I still get d:\\stuff, morestuff\x0curtherdown\thefile.mux
    – BeeBand
    Jul 2, 2010 at 15:56
  • 2
    @BeeBand: your having issues with escaping your string. '\x0c' is the form feed character. The way to create the form feed character is '\f'. If you really want the literal string '\f' you have two options: '\\f' or r'\f'.
    – deft_code
    Jul 2, 2010 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.