Mateusz Szymański
Mateusz Szymański

Reputation: 117

Split path in Python

How I could split this:

C:\my_dir\repo\branch

to:

['C:\my_dir', rest_part_of_string]

where rest_part_of_string can be one string or could be splitted every \. I don't care about rest, i just want first two elements together.

Upvotes: 3

Views: 3710

Answers (4)

Cong Ma
Cong Ma

Reputation: 11322

You need os.path.dirname() (or os.path.split), applied recursively or iteratively, until you cannot go up in the directory hierarchy further.

In general the functions provided by os.path should work better that re-invented wheels, due to better cross-platform support. There are a large number of primitives from which you can build your own path-manipulating function.

Upvotes: 0

hiro protagonist
hiro protagonist

Reputation: 46921

python 3.4 has methods for that (note the forward slashes instead of the backslashes (or double the backslashes))

pathlib documentation

# python 3.4
from pathlib import Path

p  = Path('C:/my_dir/repo/branch')

print(p.parent)
print(p.name)

for what you need parts is interesting:

print(p.parts)
# -> ('C:', 'my_dir', 'repo', 'branch')
print('\\'.join(p.parts[:2]), ' -- ', '\\'.join( p.parts[2:])) 
# -> C:\my_dir  --  repo\branch

in python 2.7 this needs a bit more work:

import os

p = 'C:/my_dir/repo/branch'

def split_path(path):
    parts = []
    while 1:
        path, folder = os.path.split(path)
        if folder:
            parts.append(folder)
        else:
            if path:
                parts.append(path)
            break
    parts.reverse()
    return parts

parts = split_path(p)
print('\\'.join(parts[:2]), ' -- ', '\\'.join(parts[2:]))
# -> C:\my_dir  --  repo\branch

Upvotes: 2

user3636636
user3636636

Reputation: 2499

you could split the path on \ and rejoin based on index:

>>>my_path = r'C:\my_dir\repo\branch'
>>>split_path = ["\\".join(my_path.split("\\")[:2]), "\\".join(my_path.split("\\")[2:])]
['C:\\my_dir', 'repo\\branch']

>>> first, last = "\\".join(x.split("\\")[:2]), "\\".join(x.split("\\")[2:])
>>> print first, last
C:\my_dir repo\branch

Upvotes: 0

falsetru
falsetru

Reputation: 369454

Using regular expression (re module documentation):

>>> import re
>>> print(re.match(r'[^\\]+\\[^\\]+', r'C:\my_dir\repo\branch').group())
C:\my_dir

>>> re.findall(r'[^\\]+\\[^\\]+|.+', r'C:\my_dir\repo\branch')
['C:\\my_dir', '\\repo\\branch']

Upvotes: 0

Related Questions