From d71fc60862eec92ca90c3ea6ee72a94415af865d Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 17 Feb 2019 23:43:33 -0800 Subject: [PATCH] Fix a bug in pathclass.Path.relative_path when cwd is root. --- voussoirkit/pathclass.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/voussoirkit/pathclass.py b/voussoirkit/pathclass.py index 6e0b6fc..082ffb6 100644 --- a/voussoirkit/pathclass.py +++ b/voussoirkit/pathclass.py @@ -66,7 +66,7 @@ class Path: @property def depth(self): - return len(self.absolute_path.split(os.sep)) + return len(self.absolute_path.rstrip(os.sep).split(os.sep)) @property def exists(self): @@ -121,15 +121,21 @@ class Path: return '.' if self in other: - return self.absolute_path.replace(other.absolute_path, '.') + relative = self.absolute_path + relative = relative.replace(other.absolute_path, '', 1) + relative = relative.lstrip(os.sep) + relative = '.' + os.sep + relative + return relative common = common_path([other.absolute_path, self.absolute_path], fallback=None) - print(common) + if common is None: return self.absolute_path + backsteps = other.depth - common.depth backsteps = os.sep.join('..' for x in range(backsteps)) - return self.absolute_path.replace(common.absolute_path, backsteps) + unique = self.absolute_path.replace(common.absolute_path, '') + return os.path.join(backsteps, unique) def replace_extension(self, extension): extension = extension.rsplit('.', 1)[-1]