Python程序比较两个不同目录中文件的内容

2024-04-25 02:25:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想比较两组以两个不同间隔执行的结果文件(文件名保持不变),忽略头部分的差异(比如前40行),将它们放在两个不同的文件夹中。如果有人有python程序,请分享

输入:目录1(x个文件) 目录2(y个文件)

输出:相同文件总数; 存在差异的文件总数

谢谢你


Tags: 文件程序目录文件夹间隔文件名差异总数
1条回答
网友
1楼 · 发布于 2024-04-25 02:25:15

最后,下面的代码达到了目的

import os
import os.path
import sys

arr= os.listdir()
arr.sort()
FO = open('Log_Out_Res.txt', 'w')

for file in arr:
    if file.endswith('.res'):
        fopen= open(file)
# loop throgh each line in the file till get the desired string and extract the version
        for line in fopen:
            lstrip = line.strip()
            if lstrip.startswith('Test File Version:'):
                try:
                    lsplit= lstrip.split()
                    print(file, "\n Test File Version:",lsplit[3])
                    FO.writelines(file + ':\n')
                    FO.writelines(lsplit[3])
                    FO.writelines('\n')
                except:
                    break
            if lstrip.startswith('Result File %version:'):
                try:
                    linesplit= lstrip.split()
                    print(" Res File Version:",linesplit[3])
                    FO.writelines('Res File Version:'+linesplit[3])
                    FO.writelines('\n')
                except:
                    break

相关问题 更多 >