【发布时间】:2021-02-24 15:26:52
【问题描述】:
我一直在寻找一个 Python 3 脚本,它可以找出两个相同大小的文件之间的字节(或者更确切地说,它们的位置)不同。 Filecmp 只给出真/假,我发现的其他脚本都没有工作。我做了这个,虽然它适用于较小的文件,但对于大文件来说非常慢。有没有人有更好(阅读:更快)的脚本?或者一种改进我的方法以使其更快。
#Get file length
items = [a,b,c]
file = open(a,"rb")
filelength = len(file.read())
file.close()
#Create dictionaries where bytes will be stored
comp = {}
compare = {}
for i in range(0,filelength):
comp[i] = []
compare[i] = {}
#Add bytes to dictionaries
for i in items:
file = open(i,"rb")
k = file.read(1)
count = 0
while k:
comp[count].append(k)
k = file.read(1)
count = count + 1
file.close()
#Get if the byte at the same position is identical to the others or not
for i in range(0,filelength):
for n in comp[i]:
if n in compare[i]:
compare[i][n] = compare[i][n] + 1
else:
compare[i][n] = 1
#Print which byte differs
for i in compare:
if compare[i][max(compare[i], key=compare[i].get)] < len(items):
print("Byte number: " + str(i+1))
【问题讨论】:
-
我是否正确解释了您的代码,项目包含要相互比较的文件列表,并且您想知道每个文件中的哪个位置出现任何不匹配?
-
@itprorh66 是的,就是这样!
标签: python python-3.x byte