Python Forum

Full Version: File type (mode)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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.
(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?
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
(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
FYI: Standard ASCII is 8 bits, but only uses bits 0-6. Extended ASCII uses all 256 bits.