Python Forum
documentation for raw bytes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
documentation for raw bytes
#5
If a file is opened in "binary", you get  a bytes object. Otherwise you get a str object:
Assuming a "bytes.dat" file that contains Déjà vu!\n (encoded in UTF-8) the following code:
with open('bytes.dat','rb') as f:
   dat=f.read()
   print(type(dat), len(dat))
   
with open('bytes.dat','r') as f:
   dat=f.read()
   print(type(dat), len(dat))
yields:
Output:
<class 'bytes'> 11 <class 'str'> 9
Of course, sys.stdin is already opened in text mode, so it reads str objects and not bytes. But you can read the binary buffer object sys.stdin is based on. See the note here.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Messages In This Thread
documentation for raw bytes - by Skaperen - Apr-26-2017, 04:55 AM
RE: documentation for raw bytes - by nilamo - Apr-26-2017, 05:31 AM
RE: documentation for raw bytes - by Skaperen - Apr-26-2017, 06:01 AM
RE: documentation for raw bytes - by volcano63 - Apr-26-2017, 06:25 PM
RE: documentation for raw bytes - by Ofnuts - Apr-26-2017, 09:24 PM

Forum Jump:

User Panel Messages

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