Python-比较来自不同文件夹的多个文件并生成diff文件

时间:2016-05-10 15:13:09

标签: python

我想在python

中自动化下面的场景

实际 -

  • cc0023-base.txt

  • cc9038.final.txt

预期:

  • base.txt
  • final.txt

1"实际"和"预期"是同一个目录下的两个不同的文件夹。我想比较" base"和"最终"两个文件夹的文件,并在另一个文件夹中生成diff文件。 DIFF: 基diff.txt 最终diff.txt 我怎么在python中做到这一点。下面是我编写的示例代码,但是它生成了所有可能组合的diff文件。我需要将base与base和final两个文件夹的final进行比较。

    expected_files=os.listdir('expected/path')
    actual_files = os.listdir('actual/path')
    diff_files=os.listdir('diff/path')
    cr=['base.txt','final.txt']
    i=0

    for files in expected_files:
        tst=os.path.join('expected/path',files)
        with open(tst,'r')as Expected:

            for actualfile in actual_files:

                 actualpath=os.path.join('actual/path',actualfile)
                 with open(actualpath,'r') as actual:
                    diff=difflib.unified_diff(Expected.readlines(),
                                              actual.readlines(),
                                              fromfile=Expected,
                                              tofile=actual,)

                    diffpath=os.path.join('diff/path',cr[i])
                    diff_file = open(diffpath, 'w')
                    for line in diff:
                         diff_file.write(line)
                    diff_file.close()
                    i=i+1

请帮助,因为我是python的新手

1 个答案:

答案 0 :(得分:0)

您的代码中的问题在本节中:

i=0
diffpath=os.path.join('diff/path',cr[i])
diff_file = open(diffpath, 'w')
for line in diff:
     diff_file.write(line)
diff_file.close()
i=i+1

由于您在访问i之前始终将0设置为cr[i],因此始终为cr[0]

i=0移至循环开始之前,您要将值初始化为0

我想你想要这样的东西:

expected_files=os.listdir('expected/path')
actual_files = os.listdir('actual/path')
diff_files=os.listdir('diff/path')
cr=['base.txt','final.txt']
j=1

for files in expected_files:
    tst=os.path.join('expected/path',files)
    with open(tst,'r')as Expected:
        #i=0
        for i, actualfile in enumerate(actual_files):

            actualpath=os.path.join('actual/path',actualfile)
            with open(actualpath,'r') as actual:
                diff=difflib.unified_diff(Expected.readlines(),
                                          actual.readlines(),
                                          fromfile=Expected,
                                          tofile=actual,)

                diffpath=os.path.join('diff/path',cr[i])
                with open(diffpath, 'w') as diff_file:
                    for line in diff:
                        diff_file.write(line)
                #diff_file.close()
                #i=i+1

一些解释,因此enumerate(actual_files)将为您提供索引i以及列表actualfile中的数据,这样您就不必自己进行递增。 (另外值得注意的是,中断目录中的2个以上文件!)此外,您可以使用with open() as foo:语法进行写入,如图所示。