I have two lists where I have the same number of files. The files in both folders have the same names, for example:
Test1 folder:
files.txt
test.txt
name.txt
and so on. In the test2 folder I have the same named files. How can I compare for each file if the data in both files is the same? So whether:
test1 / files.txt = test2 / files.txt
test1 / test.txt = test2 / test.txt
test1 / name.txt = test2 / name.txt
import os
import difflib
test1_path = "C:/Users/admin/Desktop/test1"
test2_path = "C:/Users/admin/Desktop/test2"
if os.path.exists(test1_path):
files_test1 = []
for name in os.listdir(test1_path):
if os.path.isfile(os.path.join(test1_path, name)):
files_test1.append(os.path.join(test1_path, name))
if os.path.exists(test2_path):
files_test2 = []
for name in os.listdir(test2_path):
if os.path.isfile(os.path.join(test2_path, name)):
files_test2.append(os.path.join(test2_path, name))
Are you able to advise something?
You don't need to perform two loops. One is enough. Also you can use the filecmp
package to compare file contents.
import os
import filecmp # to compare files
test1_path = 'C:/Users/admin/Desktop/test1'
test2_path = 'C:/Users/admin/Desktop/test2'
if os.path.exists(test1_path) and os.path.exists(test2_path):
all_same = True
for name in os.listdir(test1_path):
file1 = os.path.join(test1_path, name)
file2 = os.path.join(test2_path, name)
if (
os.path.isfile(file1) and
os.path.isfile(file2) and
not filecmp.cmp(file1, file2)
):
all_same = False
print('all files are the same:', all_same)
Note that the code can be simplified if you know for sure that both directories contain the same file names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.