3

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.

1
  • use raw strings?? r"C:\my_dir\repo\branch" link Commented Aug 4, 2015 at 15:31

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

pathlib has been backported to Python 2 as pathlib2
0

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']

2 Comments

@MateuszSzymański, Welcome to Stack Overflow! There are people who have tried to answer your question. If this helped you, you can tell the community so by accepting the answer that was most useful for you.
Yeah, but i have to wait about 1 hour before i can do it. And yesterday i had no access to SO. Thanks :)
0

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

Comments

0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.