python3 二进制文件比较_Python:如何比较两个二进制文件?

shell命令cmp已经完全满足了我的需要/需要。在Python中重新设计这个功能将需要更多的努力/代码/时间。。。所以我刚从Python调用了命令:#!/usr/bin/env python2

import commands

import numpy as np

def run_cmp(filename1, filename2):

cmd = 'cmp --verbose %s %s'%(filename1, filename2)

status, output = commands.getstatusoutput(cmd) # python3 deprecated `commands` module FYI

status = status if status < 255 else status%255

if status > 1:

raise RuntimeError('cmp returned with error (exitcode=%s, '

'cmd=\"%s\", output=\n\"%s\n\")'%(status, cmd, output))

elif status == 1:

is_different = True

elif status == 0:

is_different = False

else:

raise RuntimeError('invalid exitcode detected')

return is_different, output

if __name__ == '__main__':

# create two binary files with different values

# file 1

tmp1 = np.arange(10, dtype=np.uint8)

tmp1.tofile('tmp1')

# file 2

tmp2 = np.arange(10, dtype=np.uint8)

tmp2[5] = 0xFF

tmp2.tofile('tmp2')

# compare using the shell command 'cmp'

is_different, output = run_cmp(filename1='tmp1', filename2='tmp2')

print 'is_different=%s, output=\n\"\n%s\n\"'%(is_different, output)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值