TypeError: a bytes-like object is required, not 'str' (tested fix)
TypeError: a bytes-like object is required, not 'str' means you passed text to something that works on bytes, or mixed the two in one operation. We reproduced it in Python 3.13 with files, subprocess, sockets, base64 and csv, and tested each fix.
1. Mixing str and bytes
Python 3 keeps text (str) and raw data (bytes) separate. Any bytes API given a str raises this error, though some word it differently:
import hashlib, base64
data = "hello"
tests = {
"b'x' + str": lambda: b"x" + data,
"bytes.split(str)": lambda: b"a,b".split(","),
"str in bytes": lambda: "a" in b"abc",
"hashlib.sha256(str)": lambda: hashlib.sha256(data),
"base64.b64encode(str)": lambda: base64.b64encode(data),
}
for name, f in tests.items():
try:
f()
except TypeError as e:
print(f"{name:22} TypeError: {e}")
Output:
b'x' + str TypeError: can't concat str to bytes
bytes.split(str) TypeError: a bytes-like object is required, not 'str'
str in bytes TypeError: a bytes-like object is required, not 'str'
hashlib.sha256(str) TypeError: Strings must be encoded before hashing
base64.b64encode(str) TypeError: a bytes-like object is required, not 'str'
2. Writing str to a file opened with "wb"
with open("/tmp/bl/out.bin", "wb") as f:
try:
f.write("hello")
except TypeError as e:
print("TypeError:", e)
f.write("hello".encode("utf-8")) # encode str -> bytes
with open("/tmp/bl/out.txt", "w", encoding="utf-8") as f: # or open in text mode
f.write("hello")
print(open("/tmp/bl/out.bin", "rb").read(), open("/tmp/bl/out.txt").read())
Output:
TypeError: a bytes-like object is required, not 'str'
b'hello' hello
Either encode the text, or open the file in text mode ("w") with an explicit encoding. For CSV, always use text mode with newline="".
3. subprocess output is bytes
import subprocess
out = subprocess.run(["echo", "a,b"], capture_output=True).stdout
print(repr(out))
try:
out.split(",")
except TypeError as e:
print("TypeError:", e)
print(out.decode().strip().split(",")) # decode once
print(subprocess.run(["echo", "a,b"], capture_output=True, text=True).stdout.strip().split(","))
Output:
b'a,b\n'
TypeError: a bytes-like object is required, not 'str'
['a', 'b']
['a', 'b']
Without text=True, stdout is bytes, so str methods with str arguments fail. Decode once at the boundary, or pass text=True. The same applies to urllib responses (resp.read() is bytes) and to requests' r.content (use r.text for str).
4. Sockets, and csv on a binary buffer
import socket
a, b = socket.socketpair()
try:
a.sendall("ping")
except TypeError as e:
print("TypeError:", e)
a.sendall("ping".encode())
print(b.recv(10).decode())
import csv, io
buf = io.BytesIO()
try:
csv.writer(buf).writerow(["x", 1])
except TypeError as e:
print("csv TypeError:", e)
Output:
TypeError: a bytes-like object is required, not 'str'
ping
csv TypeError: a bytes-like object is required, not 'str'
Sockets only send bytes: .encode() before sending, .decode() after receiving. csv.writer writes str, so give it io.StringIO or a text-mode file, not BytesIO. The reverse mistake is 'str' object has no attribute 'decode'.
Frequently asked questions
How do I convert str to bytes in Python 3?
s.encode('utf-8'). Convert back with b.decode('utf-8').
Why does subprocess output give a bytes-like object is required?
stdout is bytes unless you pass text=True. Decode it or pass text=True.
Why does csv.writer raise a bytes-like object is required?
The file was opened in binary mode. Open it with open(path, 'w', newline='', encoding='utf-8').
Written with AI assistance. Measured on 2026-10-22 on Debian with Python 3.13.5: bytes concat/split/in, hashlib, base64, files opened with wb, subprocess output, socket.sendall and csv on BytesIO. Every block was run verbatim and its output pasted unedited. PlainHeaders is our extension; Python is free.