Fix a bug in pathclass.Path.relative_path when cwd is root.

master
Ethan Dalool 2019-02-17 23:43:33 -08:00
parent 6c1c8fe0ab
commit d71fc60862
1 changed files with 10 additions and 4 deletions

View File

@ -66,7 +66,7 @@ class Path:
@property @property
def depth(self): def depth(self):
return len(self.absolute_path.split(os.sep)) return len(self.absolute_path.rstrip(os.sep).split(os.sep))
@property @property
def exists(self): def exists(self):
@ -121,15 +121,21 @@ class Path:
return '.' return '.'
if self in other: 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) common = common_path([other.absolute_path, self.absolute_path], fallback=None)
print(common)
if common is None: if common is None:
return self.absolute_path return self.absolute_path
backsteps = other.depth - common.depth backsteps = other.depth - common.depth
backsteps = os.sep.join('..' for x in range(backsteps)) 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): def replace_extension(self, extension):
extension = extension.rsplit('.', 1)[-1] extension = extension.rsplit('.', 1)[-1]