编写二进制文件时Python中缺少字节?

Fro*_*oob 3 python binary-data

在Python中编写二进制文件时,我似乎缺少一些字节.我已经尝试使用"write"函数和"array.tofile"函数.这是一些示例代码:

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()
Run Code Online (Sandbox Code Playgroud)

给出以下输出:

Reading data from:  strings.exe
Input File Size: 136592
Document Size: 136592
Output File Size: 135168
Missing Bytes: 1424
Run Code Online (Sandbox Code Playgroud)

我不明白为什么没有写这些字节.我已经在多个文件上尝试了这种方法,其中包含不同数量的丢失字节.

Joh*_*hin 5

你打电话之前,您不关闭输出文件os.path.getsize就可以了.你写的135168字节是33 x 4096字节块...尝试copyFile.close()而不是copyFile.close.