Open the file in bytes mode, write to it, and close the file.
>>> p = Path('my_binary_file') >>> p.write_bytes(b'Binary file contents') 20 >>> p.read_bytes() b'Binary file contents'
pathlib/Path.write_bytes
Path.write_bytes