在 Python 中,有没有比较简洁的方法来比较两个文本文件的内容是否相同? [英] In Python, is there a concise way of comparing whether the contents of two text files are the same?

查看:22
本文介绍了在 Python 中,有没有比较简洁的方法来比较两个文本文件的内容是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不在乎有什么不同.我只是想知道内容是否不同.

I don't care what the differences are. I just want to know whether the contents are different.

推荐答案

低级方式:

from __future__ import with_statement
with open(filename1) as f1:
   with open(filename2) as f2:
      if f1.read() == f2.read():
         ...

高级方式:

import filecmp
if filecmp.cmp(filename1, filename2, shallow=False):
   ...

这篇关于在 Python 中,有没有比较简洁的方法来比较两个文本文件的内容是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆