{"record":{"id":"d65a50b0eac51ced","repo":"juicedata/juicefs","slug":"a-bytes-like-object-is-required-not-type-data","errorCode":null,"errorMessage":"a bytes-like object is required, not '{type(data).__name__}'","messagePattern":"a bytes-like object is required, not '(.+?)'","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdk/python/juicefs/juicefs/juicefs.py","lineNumber":544,"sourceCode":"        else:\n            buf = b''.join(rs)\n        self.off += len(buf)\n        return buf\n\n    def readinto(self, buffer):\n        data = self.read(len(buffer))\n        if not data:\n            return 0\n        buffer[:len(data)] = data\n        return len(data)\n\n    def write(self, data):\n        \"\"\"Write the string data to the file.\"\"\"\n        self._check_closed()\n        if isinstance(data, memoryview):\n            data = data.tobytes()\n        if not isinstance(data, six.binary_type):\n            raise TypeError(f\"a bytes-like object is required, not '{type(data).__name__}'\")\n        if not self.writable():\n            raise io.UnsupportedOperation('not writable')\n\n        if not data:\n            return 0\n        if self.append:\n            self.off = self.length\n        n = self.lib.jfs_pwrite(c_int64(_tid()), c_int32(self.fd), data, c_int32(len(data)), c_int64(self.off))\n        self.off += n\n        if self.off > self.length:\n            self.length = self.off\n        return n\n\n    def seek(self, offset, whence=0):\n        \"\"\"Set the stream position to the given byte offset.\n        offset is interpreted relative to the position indicated by whence.\n        The default value for whence is SEEK_SET.\"\"\"\n        self._check_closed()","sourceCodeStart":526,"sourceCodeEnd":562,"githubUrl":"https://github.com/juicedata/juicefs/blob/c9a67b23e8e08ec23ec331aa6f1675e2319e921c/sdk/python/juicefs/juicefs/juicefs.py#L526-L562","documentation":"write() only accepts bytes (or memoryview, which it converts). Passing any other type — notably str — raises TypeError(\"a bytes-like object is required, not '{type}'\"). The file is opened in binary mode and backed by a C buffer, so raw bytes are required.","triggerScenarios":"Calling f.write('hello') on a file opened with 'wb'/'ab'; passing an int, bytearray wrapped object, or numpy scalar; writelines() forwarding a list of str items.","commonSituations":"Mixing text and binary file objects after switching mode from 'w' to 'wb'; writing f-strings or CSV rows directly; migrating code from pathlib/os file APIs that accept str.","solutions":["Encode strings before writing: f.write(data.encode('utf-8'))","Open the file in text mode ('w') if you want to write str","Convert other buffer types with bytes(data) or memoryview(data).tobytes()"],"exampleFix":"// before\nwith juicefs.open(path, 'wb') as f:\n    f.write('hello')\n// after\nwith juicefs.open(path, 'wb') as f:\n    f.write('hello'.encode('utf-8'))","handlingStrategy":"type-guard","validationCode":"if not isinstance(data, (bytes, bytearray, memoryview)):\n    data = data.encode('utf-8') if isinstance(data, str) else bytes(data)","typeGuard":"def is_bytes_like(x) -> bool:\n    return isinstance(x, (bytes, bytearray, memoryview))","tryCatchPattern":"try:\n    f.write(data)\nexcept TypeError:\n    f.write(str(data).encode('utf-8'))","preventionTips":["Open in 'w' (text) mode when writing str","Encode all strings at the boundary before binary writes","Type-annotate write helpers with bytes to catch misuse early"],"tags":["python","typeerror","bytes","binary-write"],"backgroundTag":"type-mismatch","analyzedSha":"c9a67b23e8e08ec23ec331aa6f1675e2319e921c","analyzedAt":"2026-09-06T17:55:48.476Z","contentChangedAt":"2026-09-06T17:55:48.476Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}