Failure in writing binary text to file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Failure in writing binary text to file (/thread-28049.html) |
Failure in writing binary text to file - Gigux - Jul-02-2020 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: So there has been no encoding.What is the right way to write text to a binary file? RE: Failure in writing binary text to file - Gribouillis - Jul-02-2020 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. RE: Failure in writing binary text to file - bowlofred - Jul-02-2020 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!' RE: Failure in writing binary text to file - Gigux - Jul-04-2020 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: 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: 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: Essentially, cat should not be able to read the bin file (or return random ASCII codes), if possible.What am I missing? RE: Failure in writing binary text to file - Gribouillis - Jul-04-2020 Are you trying to encrypt the file? Can you explain the goal of all this? RE: Failure in writing binary text to file - Gigux - Jul-04-2020 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...
RE: Failure in writing binary text to file - ndc85430 - Jul-04-2020 Encoding (whether base 64 or otherwise) is not encryption. See, e.g. this. RE: Failure in writing binary text to file - Gigux - Jul-04-2020 Fair enough, so it is really encryption I am after. Thank you |