juicedata/juicefs · error · TypeError

a bytes-like object is required, not '{type(data).__name__}'

Error message

a bytes-like object is required, not '{type(data).__name__}'

What it means

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.

Source

Thrown at sdk/python/juicefs/juicefs/juicefs.py:544

        else:
            buf = b''.join(rs)
        self.off += len(buf)
        return buf

    def readinto(self, buffer):
        data = self.read(len(buffer))
        if not data:
            return 0
        buffer[:len(data)] = data
        return len(data)

    def write(self, data):
        """Write the string data to the file."""
        self._check_closed()
        if isinstance(data, memoryview):
            data = data.tobytes()
        if not isinstance(data, six.binary_type):
            raise TypeError(f"a bytes-like object is required, not '{type(data).__name__}'")
        if not self.writable():
            raise io.UnsupportedOperation('not writable')

        if not data:
            return 0
        if self.append:
            self.off = self.length
        n = self.lib.jfs_pwrite(c_int64(_tid()), c_int32(self.fd), data, c_int32(len(data)), c_int64(self.off))
        self.off += n
        if self.off > self.length:
            self.length = self.off
        return n

    def seek(self, offset, whence=0):
        """Set the stream position to the given byte offset.
        offset is interpreted relative to the position indicated by whence.
        The default value for whence is SEEK_SET."""
        self._check_closed()

View on GitHub (pinned to c9a67b23e8)

Solutions

  1. Encode strings before writing: f.write(data.encode('utf-8'))
  2. Open the file in text mode ('w') if you want to write str
  3. Convert other buffer types with bytes(data) or memoryview(data).tobytes()

Example fix

// before
with juicefs.open(path, 'wb') as f:
    f.write('hello')
// after
with juicefs.open(path, 'wb') as f:
    f.write('hello'.encode('utf-8'))
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(data, (bytes, bytearray, memoryview)):
    data = data.encode('utf-8') if isinstance(data, str) else bytes(data)

Type guard

def is_bytes_like(x) -> bool:
    return isinstance(x, (bytes, bytearray, memoryview))

Try / catch

try:
    f.write(data)
except TypeError:
    f.write(str(data).encode('utf-8'))

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of juicedata/juicefs@c9a67b23e8 (2026-09-06). Data as JSON: /api/errors/d65a50b0eac51ced. Report an issue: GitHub.