Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python-stdlib/pathlib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ def with_suffix(self, suffix):
index = -len(self.suffix) or None
return Path(self._path[:index] + suffix)

def expanduser(self):
if self._path == "~" or self._path.startswith("~" + _SEP):
return Path(os.getenv("HOME") + self._path[1:])
if self._path[0] == "~":
raise RuntimeError("User home directory expansion not supported.")
return self

@property
def stem(self):
return self.name.rsplit(".", 1)[0]
Expand Down
8 changes: 8 additions & 0 deletions python-stdlib/pathlib/tests/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,11 @@ def test_with_suffix(self):
self.assertTrue(Path("foo/test").with_suffix(".tar") == Path("foo/test.tar"))
self.assertTrue(Path("foo/bar.bin").with_suffix(".txt") == Path("foo/bar.txt"))
self.assertTrue(Path("bar.txt").with_suffix("") == Path("bar"))

def test_expanduser(self):
self.assertFalse(str(Path("~").expanduser()) == "~")
self.assertTrue(str(Path("~").expanduser()) == os.getenv("HOME"))
self.assertTrue(str(Path("~/foo").expanduser()) == os.getenv("HOME") + "/foo")

with self.assertRaises(RuntimeError):
Path("~foo").expanduser()