Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - Reading all kinds of files in different encodings

I built a Python steganographer that hides UTF-8 text in images and it works fine for it. I was wondering if I could encode complete files in images. For this, the program needs to read all kinds of files. The problem is that not all files are encoded with UTF-8 and therefore, you have to read them with:

file = open('somefile.docx', encoding='utf-8', errors='surrogateescape')

and if you copy it to a new file and read them then it says that the files are not decipherable. I need a way to read all kinds of files and later write them so that they still work. Do you have a way to do this in Python 3?

Thanks.

like image 341
TheRandomGuy Avatar asked Jan 24 '26 14:01

TheRandomGuy


1 Answers

Change your view. You don't "hide UTF-8 text in images". You hide bytes in images.

These bytes could be - purely accidentally - interpretable as UTF-8-encoded text. But in reality they could be anything.

Reading a file as text with open("...", encoding="...") has the hidden step of decoding the bytes of the file into string. This is convenient when you want to treat the file contents as string in your program.

Skip that hidden decoding step and read the file as bytes: open("...", "rb").

like image 76
Tomalak Avatar answered Jan 26 '26 07:01

Tomalak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!