When writing binary files in Python I seem to be missing some bytes. I’ve tried this with the “write” function and with the “array.tofile” function. Here is some example code:
import zlib, sys, os, array
from struct import unpack
from array import array
inputFile = 'strings.exe'
print "Reading data from: ", inputFile
print 'Input File Size:', os.path.getsize(inputFile)
f = open(inputFile, 'rb')
#compressedDocument =
document = f.read()
documentArray = array('c', document)
print 'Document Size:', len(documentArray)
copyFile = open( 'Copy of ' + inputFile, 'wb')
documentArray.tofile(copyFile)
#copyFile.write(document)
copyFile.close
print 'Output File Size:', os.path.getsize('Copy of ' + inputFile)
print 'Missing Bytes:', os.path.getsize(inputFile) - os.path.getsize('Copy of ' + inputFile)
f.close()
Gives the following output:
Reading data from: strings.exe
Input File Size: 136592
Document Size: 136592
Output File Size: 135168
Missing Bytes: 1424
I don’t understand why those bytes aren’t being written. I’ve tried this on multiple files with a varying number of missing bytes.
If you actually try to compare the two binary files (if you are under unix you use the
cmpcommand) you will see the two files are identical.EDIT: As correctly pointed out by John in his answer, the difference in byte size is due to not closing the file before measuring its length. The correct line in the code should be
copyFile.close()[invoking the method] instead ofcopyFile.close[which is the method object].