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
Shutting Down Computer (Linux) Using Python
Popen.Communicate() Throws Oserror: "[Errno 10] No Child Processes"
How to Make Sure If Some HTML Elements Are Loaded for Selenium + Python
Should I Use 'Has_Key()' or 'In' on Python Dicts
How to Find Tag with Particular Text with Beautiful Soup
Why Is Plotting with Matplotlib So Slow
How to Exit Linux Terminal Using Python Script
How to Create a Namespace Package in Python
How to Use the Same Python Virtualenv on Both Windows and Linux
How to Reliably Open a File in the Same Directory as the Currently Running Script
How to Execute Python File in Linux
Pelican 3.3 Pelican-Quickstart Error "Valueerror: Unknown Locale: Utf-8"
Authenticate from Linux to Windows SQL Server with Pyodbc
What Is the Pythonic Way to Avoid Default Parameters That Are Empty Lists
Pandas Groupby Mean - into a Dataframe