Isky Mathews
Isky Mathews

Reputation: 250

Why am I only writing 28,672 bits to this file?

I have been working on a project where it is necessary to program a binary file, of a certain kind, to a AT28C256 chip. The specifics are not important beyond the fact that the file needs to be 32,768 bytes in size (exactly).

I have some "minimal problem" code here:

o = open("images.bin", "wb")
c = 0
for i in range(256):
    for j in range(128):
        c += 1
        o.write(chr(0).encode('utf-8'))
print(c)

This, to me, would appear to write 32,768 bytes to a file (the split into i,j is necessary because I need to write an image to the device) as 128*256 = 32768. And the output of c is 32768!

But the file it creates is 28672 bytes long! The fact that this is 7000 in hex has not passed me by but I'm not sure why this is happening. Any ideas?

Upvotes: 3

Views: 103

Answers (1)

blhsing
blhsing

Reputation: 106553

You should call o.close() to flush the write buffer and close the file properly.

Upvotes: 2

Related Questions