Python Forum
Failure in writing binary text to file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Failure in writing binary text to file
#1
I would like to write some text to file but coded as binary so that the text is not immediately visible with a text editor.
I followed this tutorial, but:

Output:
# in python3 with open('mybinfile.bin', 'wb') as fl: text = 'Hello InfinityQuest!' fl.write(text.encode('utf-8')) # in shell $ cat mybinfile.bin Hello InfinityQuest!
So there has been no encoding.
What is the right way to write text to a binary file?
Reply
#2
If you encode an ascii string with the UTF-8 encoding, it does not change a single byte. For non ascii characters there are more bytes.
>>> "Hello InfinityQuest!".encode('utf-8')
b'Hello InfinityQuest!'
>>> "hétérogénéité".encode('utf-8')
b'h\xc3\xa9t\xc3\xa9rog\xc3\xa9n\xc3\xa9it\xc3\xa9'
Your way to write binary files is perfect.
Reply
#3
If you want the text to be hard to read, you'll need a different encoding than UTF-8, and you'll need an encoding that an editor won't randomly understand (like UTF-16).

You could run it through base64 before writing it out.

>>> import base64
>>> "Hello InfinityQuest!".encode()
b'Hello InfinityQuest!'
>>> base64.b64encode("Hello InfinityQuest!".encode())
b'SGVsbG8gSW5maW5pdHlRdWVzdCE='
>>> base64.b64decode(b'SGVsbG8gSW5maW5pdHlRdWVzdCE=')
b'Hello InfinityQuest!'
>>> base64.b64decode(b'SGVsbG8gSW5maW5pdHlRdWVzdCE=').decode()
'Hello InfinityQuest!'
Reply
#4
Thank you for the tips. I tried the following:

import base64

text = base64.b64encode("Hello InfinityQuest!".encode())
print(text)
print("writing")
with open('mybinfile.bin', 'wb') as fl:
    fl.write(text)
print("done")
and I got:
Output:
b'SGVsbG8gSW5maW5pdHlRdWVzdCE=' writing done $ cat mybinfile.bin SGVsbG8gSW5maW5pdHlRdWVzdCE=
The encoding is done, it looked binary within python, but then the file -- despite the extension -- is read as text with cat. I then tried to further encode in binary directly:
with open('mybinfile.bin', 'wb') as fl:
    fl.write(text.encode('utf-16'))
print("done")
but I got:
Error:
b'SGVsbG8gSW5maW5pdHlRdWVzdCE=' writing Traceback (most recent call last): File "./emailer.py", line 11, in <module> fl.write(text.encode('utf-16')) AttributeError: 'bytes' object has no attribute 'encode'
and if I try to write directly as:
with open('mybinfile.bin', 'w') as fl:
    # fl.write(text.encode('utf-16'))
    fl.write(text)
print("done")
I get:
Error:
b'SGVsbG8gSW5maW5pdHlRdWVzdCE=' writing Traceback (most recent call last): File "./emailer.py", line 11, in <module> fl.write(text) TypeError: write() argument must be str, not bytes
Essentially, cat should not be able to read the bin file (or return random ASCII codes), if possible.
What am I missing?
Reply
#5
Are you trying to encrypt the file? Can you explain the goal of all this?
Reply
#6
Yes, it is a kind of encryption. I would like to avoid that somebody could simply use a text editor or xat to read the file. I could encrypt with some specific shell-based apps, but I am looking for simple implementation in python. I thought that writing a binary would do the trick...
Reply
#7
Encoding (whether base 64 or otherwise) is not encryption. See, e.g. this.
Reply
#8
Fair enough, so it is really encryption I am after. Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failure to run source command middlestudent 2 690 Sep-22-2023, 01:21 PM
Last Post: buran
  How do I read and write a binary file in Python? blackears 6 6,513 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,114 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Script File Failure-Path Error? jerryf 13 3,450 Nov-30-2022, 09:58 AM
Last Post: jerryf
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,369 Sep-27-2022, 01:38 PM
Last Post: buran
  Dickey Fuller failure Led_Zeppelin 4 2,609 Sep-15-2022, 09:07 PM
Last Post: Led_Zeppelin
  Writing into 2 text files from the same function paul18fr 4 1,672 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Writing to json file ebolisa 1 997 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,657 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Writing to External File DaveG 9 2,486 Mar-30-2022, 06:25 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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