It is possible to read/write the binary of any file with using Python?

open(file, "b") only works for windows
"rb" "rb+" "wb" "wb+" "ab" "ab+" works but .read() just prints the file contents not its binary

Recommended Answers

All 10 Replies

but .read() just prints the file contents not its binary

What do you mean exactly ? Give a binary example (you can attach a zip or an image to a post).
read() does not print anything, it reads the binary file's content. Please explain what you're expecting and what you get.

Check this ...

# tested with Python33

mystr = """\
test of
a multiline
string
"""

# encode string to <class 'bytes'> or byte string
mybytes = mystr.encode("utf8")

fname = "aatest.dat"
# write out the byte string
with open(fname, "wb") as fout:
    fout.write(mybytes)

# read the data back in
with open(fname, "rb") as fin:
    mydata = fin.read()

# testing ...
print(mydata)

''' result ...
b'test of\na multiline\nstring\n'
'''

That also works python 2.7.3 [linux]
I only get this results although

>>> print(mydata)
test of
a multiline
string

>>> 

By binary I meant read/write 0"s and 1"s of the file.

What is a .dat file anyways?

By binary I meant read/write 0"s and 1"s of the file

The 0's and 1's are read, but you can't see them. They are read in bytes (for example the byte 'a' is stored as 8 bits 01100001). If you want to see them, you can use this snippet. For example

>>> mystr = """\
... test of
... a multiline
... string
... """
>>> a2bits(mystr)
'011101000110010101110011011101000010000001101111011001100000101001100001001000000110110101110101011011000111010001101001011011000110100101101110011001010000101001110011011101000111001001101001011011100110011100001010'
>>> 

What is a .dat file anyways?

It is nothing special, .dat means data. It is used for data files without a specific file extension.

But how can I do something like write a file consisting of only 001100010010011110100001101101110011 and also read the file I make and have output the same like that 0"s and 1"s
This where I find it and came gave to me idea
I want to make it into a file and run it to see what happens to my machine
FUTURAMS FTW

Install the bitstring module from pypi (type pip install bitstring in a terminal or cmd). You can easily play with strings of bits. Here is an example with python 2.7

from bitstring import BitString
>>> s = BitString(bin='011101000110010101110011011101000010000001101111011001100000101001100001001000000110110101110101011011000111010001101001011011000110100101101110011001010000101001110011011101000111001001101001011011100110011100001010')
>>> s
BitStream('0x74657374206f660a61206d756c74696c696e650a737472696e670a')
>>> print s.bytes
test of
a multiline
string

>>> 

You could create the binary file with

s = BitString(bin = '001100010010011110100001101101110011')
with open('myfile.dat', 'wb') as fout:
    fout.write(s.bytes)

Then you could read the file with

s = BitArray(filename = 'myfile.dat')

See the doc here.

it didn't work Cannot interpret as bytes unambiguously - not multiple of 8 bits
Is there another way to do this? I notice you converted the string to bytes so it writes as english but if I try without .bytes I get the error message must be convertible to a buffer, not BitStream
And what is BitStream('0x3127a1b73') is that what hex or assembly is? why doesn't s just print the 0"s and 1"s?
i haz 2 b tha Royale Programmar!!!

from bitstring import Bits
s = Bits(bin='001100010010011110100001101101110011')

s returns Bits('0x3127a1b73')

In the docs it has this example
>>> s3 = Bits(bin='0b001000110100')
But it has a b as the second char of the str?!?!?

Yes, python traditionally represents binary strings with the prefix '0b'. In this case, you can ignore this prefix. Passing the same string without '0b' gives the same result. In the same way, hexadecimal strings start with '0x'.

Do I need to use a compiler to assemble a file from with pure 1"s and 0"s?

I found xxd -b "File Name" command from the my terminal, I"m not sure a what it does excatly
which it always starts with 0000000: and then all of the binary separated its by spaces every 8 digits, I am not sure if the last set of binary are for anything specific or apart of the text
it always appends there a 00001010 unless the file is completely empty then nothing is returned at all, not even the 00000000: part just as if nothing even happend
but I have a UFO file that I can"t run or open, it returns 0000000: 00000001 00000010 00000011 ...
Why it ends with 00000011?
it appeared on my Desktop since I began this my quest although somehow I don"t recall what or where or when it was comes from
it seems this knowledge will come in handy for dealing with obscure files for as long as they are only a few bytes each

I just did research experiments and testing to do make a file pipe or dump

askdfjaoisgk in file and then it says

0000000: 01100001 01110011 01101011 01100100 01100110 01101010  askdfj
0000006: 01100001 01101111 01101001 01110011 01100111 01101011  aoisgk
000000c: 00001010                                               .

but when I empty the file and then make a newline by press enter or return, then that binary is
0000000: 00001010 00001010 ..
So I have found probably the text editor is add a newline and that"s why my file always has 00001010

This informations is for my digital experiences for reflections and hope of one day do make the progarmming in the machines own native languages plus plus it"s their code dialects.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.