Python Forum
python 3: TypeError: a bytes-like object is required, not 'str'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python 3: TypeError: a bytes-like object is required, not 'str'
#1
hey all,

i know this might be a dull question, but i dont get it...

this works in python2

#!/usr/bin/env python

import subprocess

proclist = subprocess.check_output(['ps', 'uaxw']).splitlines()
cfeagent_procs = [proc for proc in proclist if 'cf-agent' in proc]
cfeagent_count = len(cfeagent_procs)
print(cfeagent_count)
if cfeagent_count > 10:
    print("agents high")
Output:
root@aixhemadbprc2: /root # ./process_count.py 145 agents high
but not in python3

Error:
root@aixhemadbprc2: /root # ./process_count.py Traceback (most recent call last): File "./process_count.py", line 6, in <module> cfeagent_procs = [proc for proc in proclist if 'cf-agent' in proc] File "./process_count.py", line 6, in <listcomp> cfeagent_procs = [proc for proc in proclist if 'cf-agent' in proc] TypeError: a bytes-like object is required, not 'str'
sorry i need a hint...

wbr

chris
Larz60+ write Jul-09-2021, 05:17 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
subprocess.check_output(['ps', 'uaxw']).splitlines()
Will output raw bytes because the encoding is missing.
This is the default behavior.

subprocess.check_output(['ps', 'uaxw'], encoding="utf8").splitlines()
Will decode as utf8 and return a str.

Docs: https://docs.python.org/3/library/subpro...eck_output
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
thank you!
Reply
#4
Your code worked in Python 2 because strings were made up of bytes. In Python 3 strings are made up of Unicode characters. This is a good thing, but it does mean that you'll need to encode/decode when working with legacy software that still thinks ASCII is king.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 270 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 446 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 679 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 920 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,262 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,046 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 3,838 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,373 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  API Post issue "TypeError: 'str' object is not callable" makeeley 2 1,834 Oct-30-2022, 12:53 PM
Last Post: makeeley
  "SUMIF" type query in Python (help required) BlainEillimatta 0 802 Oct-06-2022, 09:08 AM
Last Post: BlainEillimatta

Forum Jump:

User Panel Messages

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