Using pathlib's read_bytes method in Python


Python tip:

You can use read_bytes() (which handles the opening and closing of the file) from pathlib's Path to read bytes from a file instead of using with open().

Example:

# pathlib
import pathlib
file_bytes = pathlib.Path("file.pdf").read_bytes()


# with open
with open("file.pdf", "rb") as file:
    file_bytes = file.read()