python3 filecmp返回错误,即使是艰难的文件是标识

2024-10-03 15:32:32 发布

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

我编写了一个python3脚本,检查两个C代码的输出文本文件是否相同。 1个文件是“想要的”输出文本文件,我把它作为带有赋值的输入/输出文件的示例,另一个是我的C代码作为输出给出的输出文件。在

import filecmp

f1="out1.txt" #the output file i got from the assignment as example
f2="myout1.txt" #the output file my c code gave for the same input file of f1
f2_copy="myout1 (copy).txt" #copy of f2
print(filecmp.cmp(f1,f2))
print(filecmp.cmp(f2,f2_copy))

我得到的结果是:

^{pr2}$

即使文件是identical,我的输出结果是False。 唯一能让我实现的方法就是复制文件然后再复制文件cmp.cmp... 在

如果重要的话,我使用Ubunto和GCC来获取输出文件。。。在

谢谢。在

编辑1: 我做了一个函数,读取每个文件的行并比较两个列表:

def textCompare(fl1,fl2):
    file1 = open(fl1, 'r')
    file2 = open(fl2, 'r')
    lines1=file1.readlines()
    lines2=file2.readlines()
    f1.close()
    f2.close()
    if lines1 == lines2:
        return True 
    else:
        return False

正如我所见,就我的目的来说,它工作得很好。。。我的问题是这个函数和文件cmp.cmp()我的功能没有检查?在


Tags: 文件ofthe代码txtoutputfilefilecmp