Python Forum
TypeError: a bytes-like object is required, not 'str'.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: a bytes-like object is required, not 'str'.
#1
Do anyone know how to convert the byte to str. I used below code work on python2 but if I used python3 it will occur Error like this:
TypeError: a bytes-like object is required, not 'str'.

So I try to use stdout = stdout.decode("UTF-8"), but it will also pop error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa8 in position 16: invalid start byte

# -*- coding: utf-8 -*-
import telnetlib
import subprocess
import time


def Telnet_Check_reachability(ip):
    ping_count=3
    process = subprocess.Popen(['ping', ip, '-n', str(ping_count)],stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    
    process.wait()
    stdout = process.stdout.read()
   
    print (type(stdout))
    print (stdout)
    if "TTL=" in stdout:
        #print "Server reachable"
        #successful = 1
        print("1 Server reachable ")
    else:
        #print "Server unreachable"
        successful = 0
        print("0 Server unreachable")
    #return successful
ip ="8.8.8.8"
new_IPv6 = Telnet_Check_reachability(ip)
#print(new_IPv6)
Reply
#2
Please, post full traceback you get, not just the last line, in error tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Error:
Traceback (most recent call last): File "ip_check.py", line 28, in <module> new_IPv6 = Telnet_Check_reachability(ip) File "ip_check.py", line 13, in Telnet_Check_reachability stdout = stdout.encode("UTF-8") AttributeError: 'bytes' object has no attribute 'encode'
adding decode
Error:
Traceback (most recent call last): File "ip_check.py", line 28, in <module> new_IPv6 = Telnet_Check_reachability(ip) File "ip_check.py", line 13, in Telnet_Check_reachability stdout = stdout.decode("UTF-8") UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa8 in position 16: invalid start byte
Reply
#4
Adding "stdout = stdout.decode("UTF-8")" seems to be a bad choice. I would remove it.
Perhaps better to concentrate on the original message: "TypeError: a bytes-like object is required, not 'str'".
Please show us the complete message and the code where that error occurs.
Reply
#5
I change to stdout = stdout.decode("big5") and can work correct.

so python2 uses str, but python3 uses byte.
Python3 has to decode to convert byte to str. Why do we have to do that?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 374 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 501 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 738 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 985 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,336 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,074 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 4,076 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,436 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  API Post issue "TypeError: 'str' object is not callable" makeeley 2 1,904 Oct-30-2022, 12:53 PM
Last Post: makeeley
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,239 Sep-19-2022, 02:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020