老板需求:每次拷贝文件到硬盘里的时候都会由于一些莫名奇妙的原因导致文件拷贝失败,或是自动跳过该文件不进行拷贝。拷贝完成后,不清楚哪些文件没有拷贝,丢失的文件需要手动寻找,非常麻烦。
应用功能:
比对目录A和目录B,返回结果:
1.A比B多了哪些文件
2.B比A多了哪些文件
import os
def getAllFiles(path):
flist = []
for root, dirs, fs in os.walk(path):
for f in fs:
f_fullpath = os.path.join(root, f)
f_relativepath = f_fullpath[len(path):]
flist.append(f_relativepath)
return flist
def dirCompare(apath, bpath):
strA=''
strB=''
afiles = getAllFiles(apath)
bfiles = getAllFiles(bpath)
setA = set(afiles)
setB = set(bfiles)
# 处理仅出现在一个目录中的文件
onlyFiles = setA ^ setB
onlyInA = []
onlyInB = []
for of in onlyFiles:
if of in afiles:
onlyInA.append(of)
elif of in bfiles:
onlyInB.append(of)
if len(onlyInA) > 0:
strA = '只在A目录中的文件'+'<br>'+'<br>'+'<br>'
for of in sorted(onlyInA):
strA+=of+'<br>'
if len(onlyInB) > 0:
strB = '只在B目录中的文件' + '<br>'+'<br>'+'<br>'
for of in sorted(onlyInB):
strB += of + '<br>'
return strA,strB