lampslave
lampslave

Reputation: 1503

Combine strings (paths) without duplication of parts

I have two paths:

a = '/folder1/subfolder/'
b = '/subfolder/folder2'

How to combine them into

c = '/folder1/subfolder/folder2/'

in the simplest way?

Upvotes: 0

Views: 58

Answers (1)

Eugene Yarmash
Eugene Yarmash

Reputation: 149736

Checking only the last component of a, you could do:

A = a.strip('/').split('/')
B = b.strip('/').split('/')
if A[-1] == B[0]:
    print("/{}/".format("/".join(A + B[1:])))

Upvotes: 1

Related Questions