Compare two files, returning True if they are the same, False otherwise. We check size first and return False quickly if the files are different sizes. If they are the same size, we continue to generating a crc for the whole file. You might wonder: why not use Python's filecmp.cmp() instead?
(filename_a, filename_b)
| 746 | |
| 747 | |
| 748 | def filecmp(filename_a, filename_b): |
| 749 | """Compare two files, returning True if they are the same, False otherwise. |
| 750 | |
| 751 | We check size first and return False quickly if the files are different sizes. |
| 752 | If they are the same size, we continue to generating a crc for the whole file. |
| 753 | |
| 754 | You might wonder: why not use Python's filecmp.cmp() instead? The answer is |
| 755 | that the builtin library is not robust to the many different filesystems |
| 756 | TensorFlow runs on, and so we here perform a similar comparison with |
| 757 | the more robust FileIO. |
| 758 | |
| 759 | Args: |
| 760 | filename_a: string path to the first file. |
| 761 | filename_b: string path to the second file. |
| 762 | |
| 763 | Returns: |
| 764 | True if the files are the same, False otherwise. |
| 765 | """ |
| 766 | size_a = FileIO(filename_a, "rb").size() |
| 767 | size_b = FileIO(filename_b, "rb").size() |
| 768 | if size_a != size_b: |
| 769 | return False |
| 770 | |
| 771 | # Size is the same. Do a full check. |
| 772 | crc_a = file_crc32(filename_a) |
| 773 | crc_b = file_crc32(filename_b) |
| 774 | return crc_a == crc_b |
| 775 | |
| 776 | |
| 777 | def file_crc32(filename, block_size=_DEFAULT_BLOCK_SIZE): |
nothing calls this directly
no test coverage detected