小编Fro*_*oob的帖子

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

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

python binary-data

3
推荐指数
1
解决办法
1060
查看次数

标签 统计

binary-data ×1

python ×1