Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General question about Binary files

I am a beginner and I am having trouble in grasping binary files. When I write to a file in binary mode (in python), I just write normal text. There is nothing binary about it. I know every file on my computer is a binary file but I am having trouble distinguishing between files written in binary mode by me and files like audio, video etc files that show up as gibberish if I open them in a text editor.

How are files that show up as gibberish created? Can you please give an example of a small file that is created like this, preferably in python?

I have a feeling I am asking a really stupid question but I just had to ask it. Googling around didn't help me.

like image 921
stirredo Avatar asked Mar 26 '11 17:03

stirredo


1 Answers

When I write to a file in binary mode (in python), I just write normal text.

You'll have to change your approach when you upgrade to Python 3.x:

>>> f = open(filename, 'wb')
>>> f.write("Hello, world!\n")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be bytes or buffer, not str
>>> f.write(b"Hello, world!\n")
14

But your question isn't really about binary files. It's about str.

In Python 2.x, str is a byte sequence that has an overloaded meaning:

  • A non-Unicode string, or
  • Raw binary data (like pixels in an image).

If you print the latter as it were the former, you get gibberish.

Python 3.x got rid of this double meaning by introducing a separate bytes type for binary data, leaving str unambiguously as a text string (and making it Unicode).

like image 127
dan04 Avatar answered Oct 29 '22 16:10

dan04