Python Forum
how to solve "a bytes-like object is required, not 'str'" error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to solve "a bytes-like object is required, not 'str'" error
#1
Hi All,

Am getting the following error, while writing a string to process using write method in python 3, and it works very well for python 2.

I have googled it and found encode() and decode. When I encode the string to utf-8 while writing, it executes without an error, however the output returns is not as expected (not as python 2 output).

This is my code snippet
process.stdin.write('exit\n')
getting the following error
 File "test5.py", line 8, in <module>
process.stdin.write('exit\n'))
TypeError: a bytes-like object is required, not 'str'
Could any one please suggest/help me out to solve this issue, would be much appreciated. Thank you.

Regards,
Maiya
Reply
#2
Hi,

What is wrong when you write
process.stdin.write('exit\n'.encode())
?
Reply
#3
Please put the code between the pair of python tags (there's an opening one and a closing one).

You should probably be targeting python3. Is there a reason that you care about how it runs under python2?

I don't see anything in your post that shows the output or how it's different from what you want it to be when encoded.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,154 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  Need help with 'str' object is not callable error. Fare 4 775 Jul-23-2023, 02:25 PM
Last Post: Fare
  TypeError: a bytes-like object is required ZeroX 13 3,835 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  TypeError: a bytes-like object is required, not 'str' - Help Please. IanJ 3 4,670 Aug-29-2022, 05:53 PM
Last Post: deanhystad
  Error in Int object is not subscript-able. kakut 2 1,131 Jul-06-2022, 08:31 AM
Last Post: ibreeden
Question how to solve `'TypeError: 'int' object is not iterable`? netanelst 2 1,521 May-24-2022, 12:03 PM
Last Post: deanhystad
  bytes object saved as .mp4 jttolleson 10 5,747 Feb-25-2022, 02:42 PM
Last Post: jttolleson
  how can I solve fsolve function error? troddydeeneeeeeey 3 2,384 Oct-14-2021, 07:07 PM
Last Post: deanhystad
  error : "Microsoft Visual C++ 14.0 is required. " Even its installed Barak 4 3,923 Oct-13-2021, 10:39 PM
Last Post: Underscore
  python 3: TypeError: a bytes-like object is required, not 'str' wardancer84 3 6,372 Jul-09-2021, 05:55 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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