Typeerror: a Bytes-Like Object Is Required, Not 'Str'

TypeError: a bytes-like object is required, not 'str' when writing to a file in Python 3

You opened the file in binary mode:

with open(fname, 'rb') as f:

This means that all data read from the file is returned as bytes objects, not str. You cannot then use a string in a containment test:

if 'some-pattern' in tmp: continue

You'd have to use a bytes object to test against tmp instead:

if b'some-pattern' in tmp: continue

or open the file as a textfile instead by replacing the 'rb' mode with 'r'.

phpserialize : a bytes-like object is required, not 'str'

The type of temp seems to be string which means it contains utf-8 character.
But you need a byte-like object.

To convert a string into a byte-like object you need to encode it.

>>> temp='whatever'
>>> print(temp)
whatever
>>> type(temp)
<class 'str'>

>>> btemp = temp.encode('utf-16')
>>> print(btemp)
b'\xff\xfew\x00h\x00a\x00t\x00e\x00v\x00e\x00r\x00'
>>> type(btemp)
<class 'bytes'>

You need to look for what kind of encoding you will need for japanese characters.

Error TypeError: a bytes-like object is required, not 'int'

Integer list is not bytes-like, but you can simply cast it into bytes.

oldfile = open("oldfile", 'rb')
l = list(oldfile.read())
out = open("newfile", 'wb')
for i in l:
out.write(bytes([i]))

Although the above should solve your problem, FYI, you should use struct.pack('B', ...) for unsigned byte (which is [0..255]), 'b' for signed byte (which is [-128..127]).

TypeError: a bytes-like object is required, not 'str' when I'm inputting command in console

This error is because python expects bytes not string to be sent via socket.

Hence you have to convert strings to bytes before sending that. you can use encode() to convert string to bytes and use decode() to convert bytes received into string.

I have updated your code, Please refer below, this should work fine.

import socket
HOST = '0.0.0.0'
PORT = 12345

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))

server_socket.listen(5)
print("\n[*] Listening on port " +str(PORT)+ ", waiting for connexions. ")

client_socket, (client_ip, client_port) = server_socket.accept()
print("[*] Client " +client_ip+ " connected \n")

while True:
try:
command = input(client_ip+ ">")
if(len(command.split()) != 0):
client_socket.send(command.encode('utf-8')) #Encoding required here

else:
continue

except(EOFError):
print("Invalid input, type 'help' to get a list of implented commands. \n")

continue

if(command == "quit"):
break

data = client_socket.recv(1024)
print(data.decode('utf-8') + "\n") #Decoding required here

client_socket.close()

TypeError a bytes-like object is required, not 'str' (Pydub)

Error message seems pretty straightforward : AudioSegment class constructor is expecting a bytes object and you are passing a String instead.

According to the pydub doc, you could simply call from_wav() method by passing your filepath as a string.

Example (from the doc) :

song = AudioSegment.from_wav("never_gonna_give_you_up.wav")


Related Topics



Leave a reply



Submit