Skip to content

Instantly share code, notes, and snippets.

@LouiS0616
Created August 4, 2018 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LouiS0616/bd4e1bcea03c6f4f52e7d88fc97da555 to your computer and use it in GitHub Desktop.
Save LouiS0616/bd4e1bcea03c6f4f52e7d88fc97da555 to your computer and use it in GitHub Desktop.
from pathlib import Path
_Path = type(Path())
class Path(_Path):
def __iter__(self):
if self.is_dir():
return self.iterdir()
raise TypeError
def with_stem(self, new_stem):
"""
>>> p = Path('spam/ham/egg.py')
>>> p.with_stem('hoge') == Path('spam/ham/hoge.py')
True
"""
return self.with_name(
new_stem + self.suffix
)
def rename(self, arg):
if isinstance(arg, str):
_Path.rename(self, arg)
return
if callable(arg):
new_name = arg(self.stem)
_Path.rename(
self, self.with_stem(new_name)
)
return
raise TypeError
if __name__ == '__main__':
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment