pathlib integrates seemlessly with open only in Python 3.6 and later. From Python 3.6’s release notes:
The built-in
open()function has been updated to acceptos.PathLikeobjects, as have all relevant functions in theosandos.pathmodules, and most other functions and classes in the standard library.
To get it to work in Python 3.5 and Python 3.6, just convert the object to a string:
contents = open(str(filename), "r").read()