ValueError: PureWindowsPath('c:/') has an empty name
+.. method:: PurePath.with_stem(stem)
+
+ Return a new path with the :attr:`stem` changed. If the original path
+ doesn't have a name, ValueError is raised::
+
+ >>> p = PureWindowsPath('c:/Downloads/draft.txt')
+ >>> p.with_stem('final')
+ PureWindowsPath('c:/Downloads/final.txt')
+ >>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
+ >>> p.with_stem('lib')
+ PureWindowsPath('c:/Downloads/lib.gz')
+ >>> p = PureWindowsPath('c:/')
+ >>> p.with_stem('')
+ Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+ File "/home/antoine/cpython/default/Lib/pathlib.py", line 861, in with_stem
+ return self.with_name(stem + self.suffix)
+ File "/home/antoine/cpython/default/Lib/pathlib.py", line 851, in with_name
+ raise ValueError("%r has an empty name" % (self,))
+ ValueError: PureWindowsPath('c:/') has an empty name
+
+ .. versionadded:: 3.9
+
+
.. method:: PurePath.with_suffix(suffix)
Return a new path with the :attr:`suffix` changed. If the original path
self.assertRaises(ValueError, P('a/b').with_name, 'c/')
self.assertRaises(ValueError, P('a/b').with_name, 'c/d')
+ def test_with_stem_common(self):
+ P = self.cls
+ self.assertEqual(P('a/b').with_stem('d'), P('a/d'))
+ self.assertEqual(P('/a/b').with_stem('d'), P('/a/d'))
+ self.assertEqual(P('a/b.py').with_stem('d'), P('a/d.py'))
+ self.assertEqual(P('/a/b.py').with_stem('d'), P('/a/d.py'))
+ self.assertEqual(P('/a/b.tar.gz').with_stem('d'), P('/a/d.gz'))
+ self.assertEqual(P('a/Dot ending.').with_stem('d'), P('a/d'))
+ self.assertEqual(P('/a/Dot ending.').with_stem('d'), P('/a/d'))
+ self.assertRaises(ValueError, P('').with_stem, 'd')
+ self.assertRaises(ValueError, P('.').with_stem, 'd')
+ self.assertRaises(ValueError, P('/').with_stem, 'd')
+ self.assertRaises(ValueError, P('a/b').with_stem, '')
+ self.assertRaises(ValueError, P('a/b').with_stem, '/c')
+ self.assertRaises(ValueError, P('a/b').with_stem, 'c/')
+ self.assertRaises(ValueError, P('a/b').with_stem, 'c/d')
+
def test_with_suffix_common(self):
P = self.cls
self.assertEqual(P('a/b').with_suffix('.gz'), P('a/b.gz'))
self.assertRaises(ValueError, P('c:a/b').with_name, 'd:/e')
self.assertRaises(ValueError, P('c:a/b').with_name, '//My/Share')
+ def test_with_stem(self):
+ P = self.cls
+ self.assertEqual(P('c:a/b').with_stem('d'), P('c:a/d'))
+ self.assertEqual(P('c:/a/b').with_stem('d'), P('c:/a/d'))
+ self.assertEqual(P('c:a/Dot ending.').with_stem('d'), P('c:a/d'))
+ self.assertEqual(P('c:/a/Dot ending.').with_stem('d'), P('c:/a/d'))
+ self.assertRaises(ValueError, P('c:').with_stem, 'd')
+ self.assertRaises(ValueError, P('c:/').with_stem, 'd')
+ self.assertRaises(ValueError, P('//My/Share').with_stem, 'd')
+ self.assertRaises(ValueError, P('c:a/b').with_stem, 'd:')
+ self.assertRaises(ValueError, P('c:a/b').with_stem, 'd:e')
+ self.assertRaises(ValueError, P('c:a/b').with_stem, 'd:/e')
+ self.assertRaises(ValueError, P('c:a/b').with_stem, '//My/Share')
+
def test_with_suffix(self):
P = self.cls
self.assertEqual(P('c:a/b').with_suffix('.gz'), P('c:a/b.gz'))