using binary in python

kent nyberg kent at z-sverige.nu
Sun Nov 8 16:27:06 EST 2015


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. 
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. 
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?

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..?

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

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



More information about the Python-list mailing list