using binary in python

Larry Hudson orgnut at yahoo.com
Tue Nov 10 01:20:25 EST 2015


Your questions are somewhat difficult to answer because you misunderstand binary.  The key is 
that EVERYTHING in a computer is binary.  There are NO EXCEPTIONS, it's all binary ALL the time. 
  The difference comes about in how this binary data is displayed and manipulated.  I want to 
emphasize, ALL the DATA is binary.

On 11/08/2015 01:27 PM, kent nyberg wrote:
> Hi there,
> Lets say I want to play around with binary files in python.
> Opening and using files in python is something that I think I've sort of got the hang of.
> The thing im wondering about is binary files.
> While searching for binary and python I started reading about bin().
> I can use bin() to convert integers to binary.

No.  It doesn't convert anything.  It takes the integer data (which is internally binary) and 
gives you a _string_ representing that value in a binary format.  The same with hex() and oct() 
which give _strings_ of text corresponding to those formats.  The original integer data is 
unchanged -- and is still internally binary data.

> Now, I thought..  some way it should be possible to write "binary" files.
> That is, non ascii-files.  For example, on Linux, if I run the command 'less' on a binary;
> for example /bin/ls, then the command asks me if I really want to do it, since its a binary file.
> I know why, since the non ascii-stuff messes up the terminal, and most likely since you rarely want to
> look at a binary file with less.

Not so much that it's rare, it's just that the less (and similar) commands expect the _binary 
data_ that it is given represents text encoded in ascii, utf-8, whatever...  And ls is a 
executable program not text.  So less (or the like) tries to display that data as text, and of 
course, the result is garbage.  (GIGO)

If you really want to look at a non-text file this way, there are utilities to do this properly, 
like hexdump.

> Well, lets assume I want to write and read binary.  How is it done?
> The built in bin() function only converts integers to binary but the variable or return is still just letters right?
> Converting the integer '1' to binary with bin() return 0b1. Which is ok. Its the binary representation of integer 1.
> But since.. there is files which contains data that is not representable as ascii, then I assume it can be written.
> But can it by Python?

Of course it can.  The only difference a text file and a binary file is the way it's opened. 
Text files are opened with 'r' or 'w', while binary files are opened with 'rb' or 'wb'.  Being 
different modes, the reading/writing is handled differently.  One obvious difference, the lines 
of a text file are marked by ending them with a newline character, so it's easy to read/write 
the text line-by-line.  But the data in a binary file is completely arbitrary and is much 
trickier to handle (is an individual piece of data 1 byte, two bytes, 12,428 bytes or...)  Once 
again I'll emphasize that the _data_ in a text file is binary, it's just that these binary data 
values represent text codes.  The data in a binary file can represent anything, a program, a 
.jpg picture, .mp3 music, a company Personnel record, or anything else.

BTW, I talk about text codes as a generic term -- there are many choices of how text is encoded.

> Ok, I get the feeling now that its hard to understand my question. I assume in the C language its just matter of
> writing a complex struct with strange variables and just write it to a file. But in python..?

There is no essential difference between how C and Python handle binary files.  The principles 
are the same, only the details differ.  You can ignore comparing to C here.

> Can some one give a short explenation of a python write to file, that makes it a binary file?

A short explanation, probably not in a newsgroup post.  Try searching the web for Python binary 
files, or similar search terms.  Then sit down and play!  ;-)

> Thanks alot,  and forgive me for my stupid questions. :)
> /Kent Nyberg

It's not a stupid question, but is is based on your misunderstanding of binary.  Don't give up, 
you'll get it!  Just keep in mind that _ALL_ the underlying _DATA_ is binary, it is just how 
this data is displayed and manipulated that makes the differences.  And it is important that you 
understand the difference between the actual (binary) data and the way it is displayed -- these 
are two entirely different things.

Sorry that I'm not more specific on the 'how' to use binary files, but the subject is more 
complex than a simple explanation can properly give.

      -- Larry -=-




More information about the Python-list mailing list