好了,原理已经说清楚,下面直接上代码:
defis_file_same(file1,file2):
withopen(file1,’rb’)asf1:
content1=f1.read()
withopen(file2,’rb’)asf2:
content2=f2.read()
#iftwofileshavedifferentsize,theycann’tbesame
iflen(content1)!=len(content2):
returnFalse
else:
#iftwofileshavesamesize,comparethecontentbytebybyte
file_len=len(content1)
forposinrange(0,file_len):
#returnFalseifanybyteinsamepositionaredifferent
ifcontent1[pos]!=content2[pos]:
returnFalse
#hasnodifferent,thetwofilesaresame
returnTrue
代码竟然不给着色和缩进,还是看下图比较舒服,^_^