Python Forum
bytes not being printed as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bytes not being printed as expected
#1
i ran into an issue printing a byte string to a file object opened in binary mode and minimized it to this little 4 line script that runs into the same problem:
Output:
lt2a/forums /home/forums 1> cat -n write_bytes.py 1 #!/usr/bin/env python3 2 from sys import stdout 3 foo = open(stdout.fileno(),'wb') 4 print(b'abcdef xyz foobar',file=foo) lt2a/forums /home/forums 2> cat write_bytes.py #!/usr/bin/env python3 from sys import stdout foo = open(stdout.fileno(),'wb') print(b'abcdef xyz foobar',file=foo) lt2a/forums /home/forums 3> python3 write_bytes.py Traceback (most recent call last): File "write_bytes.py", line 4, in <module> print(b'abcdef xyz foobar',file=foo) TypeError: a bytes-like object is required, not 'str' lt2a/forums /home/forums 4>
can someone explain what is going wrong here?

the source of the issue is a program that is reading from a pipe created by subprocess.Popen() which gets bytes because it opened the pipe in binary. rather than decoding those bytes for every line i was copying, i just re-opened stdout in binary, as this example shows. but it seems to be confused when i print bytes. is print() decoding them back to strings?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Right, you can't use print with binary files. This is mentioned in the docs:
Quote:Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.

So just use sys.stdout.buffer.write() and you'll be fine.
I never really understood the point of using print() with a file anyway...
Reply
#3
i was just trying to copy, to stdout, what a child process printed to its stdout, piped to the parent. ok, so read() and write().
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  coma separator is printed on a new line for some reason tester_V 4 481 Feb-02-2024, 06:06 PM
Last Post: tester_V
  How can histogram bins be separated and reduce number of labels printed on x-axis? cadena 1 882 Sep-07-2022, 09:47 AM
Last Post: Larz60+
  Why, TypeError: expected string or bytes-like object ? JohnnyCoffee 3 18,597 May-08-2020, 04:26 AM
Last Post: bowlofred
  Reverse printed words hindubiceps 7 2,984 Apr-21-2020, 05:19 PM
Last Post: deanhystad
  I get "None" at the end of my printed result. dyshkant 3 2,634 Sep-06-2019, 06:31 PM
Last Post: dyshkant
  Change linenumber and filename printed in exceptions like #line in C kryptomatrix 2 2,659 Jul-12-2019, 06:01 AM
Last Post: Gribouillis
  Putting an array for each string that is printed to a loop ClaudioSimonetti 1 2,337 Feb-05-2019, 12:52 PM
Last Post: perfringo
  replace bytes with other byte or bytes BigOldArt 1 10,608 Feb-02-2019, 11:00 PM
Last Post: snippsat
  Why A will be printed twice in the picture Shen 3 4,074 Jul-25-2018, 01:16 PM
Last Post: stranac
  0 error but Not printed Piqurs 5 3,948 Jul-14-2018, 04:43 PM
Last Post: buran

Forum Jump:

User Panel Messages

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