如何使用python 3.6.1比较2个目录/文件夹,以解决其中的文件到文件(XML)差异



我想在 *.log 文件中记录差异,以便在 2 个目录或文件夹之间进行每个文件到文件的行比较。

开始差异

k=0
for i in dirs1:
    for j in dirs2:
        if i==j:
            with open(os.path.join(path1,os.path.basename(i)), 'r') as f1:
                with open(os.path.join(path2,os.path.basename(j)), 'r') as f2:
                    k+=1
                    print('TC'+str(k)+ ' started...')
                    print(os.path.join(path1,os.path.basename(i)), 't',os.path.join(path2,os.path.basename(j)))
                    out_file.flush()
                    name1=os.path.basename(i)
                    name2=os.path.basename(j)
                    if name1==name2:
                        diff = difflib.ndiff(f1.readlines(), f2.readlines())
                        out_file.flush()
                        testPassed=None
                        #print lines with difference
                        for line in diff:
                            if line.startswith('-') and not(line.startswith('- <ID') or line.startswith('-Created        :') or  line.startswith('-Name=') or line.startswith('- <Information') or line.startswith('- ,,Date:') or line.startswith('- ,,,')):
                                testPassed=True
                                out_file.write(line)
                                out_file.flush()
                            elif line.startswith('+') and not(line.startswith('+ <ID') or line.startswith('+Created        :') or line.startswith('+Name=') or line.startswith('+ <Information') or line.startswith('+ ,,Date:') or line.startswith('+ ,,,')):
                                testPassed=True
                                out_file.write(line)
                                out_file.flush()
                        if not testPassed ==True:
                            print('tTC'+str(k)+' Passed for: %sn' %i)
                        else:
                            print('tt***TC'+str(k)+' Failed for: %s ***n' %i)

相关内容

  • 没有找到相关文章

最新更新