When writing b开发者_StackOverflowinary 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.
You are not closing your output file before you call os.path.getsize
on it. Your 135168 bytes written is 33 x 4096 byte blocks ... try copyFile.close()
instead of copyFile.close
.
If you actually try to compare the two binary files (if you are under unix you use the cmp
command) 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 of copyFile.close
[which is the method object].
精彩评论