(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