非浅文件cmp.cmp 到底有什么作用?

发布于 2024-09-13 11:17:42 字数 518 浏览 0 评论 0原文

我正在使用Python 2.6.2。 filecmp 模块的文档说:

filecmp 模块定义了比较文件和目录的函数,以及各种可选的时间/正确性权衡。

以及 filecmp.cmp 函数的:

filecmp.cmp(f1, f2[, 浅])

比较名为 f1 和 f2 的文件,如果它们看起来相等则返回 True,否则返回 False。

除非给出了shallow并且为假,否则具有相同os.stat()签名的文件被认为是相等的。

他们没有做的是指定使用 shallow=False 获得的正确性级别是多少。那么,shallow=False 是做什么的呢?它有多正确?

I'm using Python 2.6.2. The docs for the filecmp module say:

The filecmp module defines functions to compare files and directories, with various optional time/correctness trade-offs.

and, of the filecmp.cmp function:

filecmp.cmp(f1, f2[, shallow])

Compare the files named f1 and f2, returning True if they seem equal, False otherwise.

Unless shallow is given and is false, files with identical os.stat() signatures are taken to be equal.

What they don't do is specify just what is the correctness level one obtains with shallow=False. So, what does shallow=False do? How correct is it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥︴琐丶欲为 2024-09-20 11:17:42

查阅filecmp.py 显示,如果 shallow=Falsefilecmp.cmp 首先检查 os.stat() 的一些选择属性,无论 >shallowTrueFalse。如果检查的统计属性相同,则返回 True。否则,它会检查其内部缓存以查看文件是否已在之前进行过比较。如果有,则返回True。否则,它会从两个文件中读取 BUFSIZE = 8*1024 数据块,并进行精确的内容比较,直到到达文件末尾。如果两个文件的内容完全相同,则返回 True。

Consulting the source filecmp.py reveals that if shallow=False, filecmp.cmp first checks a few select properties of os.stat(), regardless of whether shallow is True or False. If the stat properties that are examined are the same, it returns True. Else, it checks its internal cache to see if the files have already been compared earlier. If it has, it returns True. Else, it reads BUFSIZE = 8*1024 chunks of data from both files and does an exact contents comparison until it reaches the end of the file. It returns True if the two files have exactly the same contents.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文