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:
[email protected]: /root # ./process_count.py 145 agents high
but not in python3

Error:
[email protected]: /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: 'DataFrame' object is not callable using Pandas in Python sofiavlachou 1 1,449 Sep-02-2021, 03:24 PM
Last Post: buran
  TypeError: 'NoneType' object is not subscriptable syafiq14 2 1,855 Aug-26-2021, 06:18 AM
Last Post: alexaminar
  TypeError: missing 3 required positional arguments: wardancer84 9 1,241 Aug-19-2021, 04:27 PM
Last Post: deanhystad
  TypeError: run_oracle_job() missing 1 required positional argument: 'connection_strin python_student 1 550 Aug-06-2021, 08:05 PM
Last Post: SheeppOSU
  TypeError: max_value() missing 2 required positional arguments: 'alpha' and 'beta' Anldra12 2 2,601 May-15-2021, 04:15 PM
Last Post: Anldra12
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 1,738 May-02-2021, 03:45 PM
Last Post: Anldra12
  TypeError: sum() missing 1 required positional argument: 'num2' Insen 3 1,788 Jan-06-2021, 04:25 PM
Last Post: Insen
  TypeError: 'NoneType' object is not subscriptable Jmekubo 6 24,269 Sep-08-2020, 10:03 AM
Last Post: DigiTMG
  TypeError: 'type' object is not subscriptable Stef 1 1,842 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  TypeError: 'list' object is not callable Python_Mey 1 1,178 Aug-25-2020, 03:56 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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