Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to compare whether the contents of two files are the same in python

Shulou Source: shulou.com Published: 2022-06-02 08:14:51 09月17日 Update

What this article shares to you is about how to compare the contents of the two files in python is the same, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article. Without saying much, follow the editor to have a look.

Import os

Def cmp_file (F1, f2):

St1 = os.stat (F1)

St2 = os.stat (f2)

# compare file sizes

If st1.st_size! = st2.st_size:

Return False

Bufsize = 81024

With open (F1, 'rb') as fp1, open (f2,' rb') as fp2:

While True:

B1 = fp1.read (bufsize) # read data of specified size for comparison

B2 = fp2.read (bufsize)

If b1! = b2:

Return False

If not b1:

Return True

Print (cmp_file ('. / data/1.txt','. / data/2.txt'))

The above is how to compare the contents of the two files in python. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: File same two content size more knowledge article practical that is work meeting data article look knowledge point industry details information information channel follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker Shulou Tech Info Huawei Microsoft