The `pathlib` module

Python 3 comes with a new module named Pathlib. Since Python 3.6, pathlib.Path objects work nearly everywhere that os.path can be used, meaning you can integrate your new code directly into legacy code without having to rewrite anything. Pathlib makes working with paths way simpler than os.path does.

Feature spotlight:

  • Normalizes file paths for all platforms automatically
  • Has glob-like utilites (eg. Path.glob, Path.rglob) for searching files
  • Can read and write files, and close them automatically
  • Convenient syntax, utilising the / operator (e.g. Path('~') / 'Documents')
  • Can easily pick out components of a path (eg. name, parent, stem, suffix, anchor)
  • Supports method chaining
  • Move and delete files
  • And much more

More Info: