Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File type (mode)
#1
If I want to create a file which contains both ASCII Text AND Binary data, which mode is best to use when creating the file ("wt" OR "wb")? or perhaps some other mode?
Thanks
Ray
Reply
#2
You'll almost certainly want to open the file in binary mode and then control any text data by explicitly encoding it yourself into whatever is necessary.
Reply
#3
(Aug-28-2021, 07:40 PM)bowlofred Wrote: You'll almost certainly want to open the file in binary mode and then control any text data by explicitly encoding it yourself into whatever is necessary.
Thanks for the tip.
I was thinking about this issue and I thought it might make sense to select the mode based on how much of the 2 data types (text or binary) would need to be written.
If the majority is text, I thought maybe the text mode is the one to use in the open method.
Then only some small part of the data to be written, which is binary, needs to be converted so it passes the "mode" test.
The opposite argument applies if most of the date to be written is binary.

Then again, maybe my thinking is considerably confused -- what do you think?
Reply
#4
No, text mode filehandles will not cope with your binary data.

with open("somedata.bin", "wt") as f:
    f.write(b'hello')  # Throws TypeError.  Can't send bytes to text file
Reply
#5
(Aug-29-2021, 02:31 AM)bowlofred Wrote: No, text mode filehandles will not cope with your binary data.

with open("somedata.bin", "wt") as f:
    f.write(b'hello')  # Throws TypeError.  Can't send bytes to text file

I see what you are saying. Here is what I was thinking:
I wish to write records to a file where the records consist of n characters (no end of line characters). Furthermore, each n character record must be preceded by a 2 byte binary record length. So a 258 character record would be written as 2 bytes containing 0x102, followed by the 258 characters that comprise the record. If you were to look at the entire record as though it were ASCII text, it would appear to be 2 ASCII control characters (SOH, STX) followed by 258 ASCII characters.
I opened the file with mode "wt" and here is how I write the 2 byte binary record length (ln):
f.write(chr(ln>>8)) # to write the hi-order 8 bits of the length
f.write(chr(ln&0xff)) # to write the lo-order 8 bits of the length

Since each 8 bits is < 256, it can be thought of as a character with respect to the chr() function.

These 2 writes for the 2 byte record length field are the only writes that look kinda weird.
Could do a single write with concatenation of record length and text as well.

Just for the insight gained, I think I'll also do it with a binary file.

Again, thanks for your thoughts on this.
Ray
Reply
#6
FYI: Standard ASCII is 8 bits, but only uses bits 0-6. Extended ASCII uses all 256 bits.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to determine attachment file type before saving off.. cubangt 1 2,135 Feb-23-2022, 07:45 PM
Last Post: cubangt
  things that work in terminal mode but not in sublime mode alok 4 2,872 Aug-11-2021, 07:02 PM
Last Post: snippsat
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,943 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,851 Dec-06-2020, 04:14 PM
Last Post: Divya577
  Type hinting - return type based on parameter micseydel 2 2,460 Jan-14-2020, 01:20 AM
Last Post: micseydel
  What mode should i open the file to write into a *.msh file Dhanya 0 1,731 Jan-09-2020, 08:27 AM
Last Post: Dhanya
  How do I write class objects to a file in binary mode? Exsul1 7 5,725 Sep-14-2019, 09:33 PM
Last Post: snippsat
  Save code with output to HTML file type agohir 2 2,851 Mar-26-2019, 05:17 PM
Last Post: agohir
  how to check for file type in a folder SoulsKeeper 4 4,935 Sep-15-2018, 02:48 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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