[Python]比较两个zip文件的功能,一个位于FTP目录中,另一个位于我的本地计算机上 [英] [Python]Function that compares two zip files, one located in FTP dir, the other on my local machine
问题描述
我有一个创建函数的问题,该函数比较两个zip文件(如果它们相同,不仅按名称).这是我的代码示例:
I have an issue creating function that compare two zip files(if they are the same, not only by name). Here is example of my code:
def validate_zip_files(self):
host = '192.168.0.1'
port = 2323
username = '123'
password = '123'
ftp = FTP()
ftp.connect(host, port)
ftp.login(username,password)
ftp.cwd('test')
print ftp.pwd()
ftp.retrbinary('RETR test', open('test.zip', 'wb').write)
file1=open('test.zip', 'wb')
file2=open('/home/user/file/text.zip', 'wb')
return filecmp.cmp(file1, file2, shallow=True)
问题之一是第二个zip文件位于不同的位置('/home/user/file/text.zip'),我正在从python脚本所在的目录中下载该zip文件.我不是100%确定filecmp.cmp可与.zip文件一起使用.
One of the problems is that the second zip is in different location('/home/user/file/text.zip') and i am downloading the zip file in the dir where my python script is. I am not 100% sure that filecmp.cmp works with .zip files.
任何想法都很棒:)谢谢.
Any ideas would be great :) Thanks.
推荐答案
我将直接比较文件的哈希值,而不是直接比较文件.这消除了 filecmp
的依赖性,这可能-如您所说-不适用于压缩文件.
Rather than comparing the files directly, I would go ahead and compare hashed values of the files. This eliminates the dependency of filecmp
, which might -as you said - not work with zipped files.
import hashlib
def compare_files(a,b):
fileA = hashlib.sha256(open(a, 'rb').read()).digest()
fileB = hashlib.sha256(open(b, 'rb').read()).digest()
if fileA == fileB:
return True
else:
return False
这篇关于[Python]比较两个zip文件的功能,一个位于FTP目录中,另一个位于我的本地计算机上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!