Python Forum

Full Version: Failure in writing binary text to file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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.
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!'
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?
Are you trying to encrypt the file? Can you explain the goal of all this?
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...
Encoding (whether base 64 or otherwise) is not encryption. See, e.g. this.
Fair enough, so it is really encryption I am after. Thank you