在两个文件夹中打印不同文件时出现问题
import os
def compare_files(file1, file2):
# Function to compare files
pass
def compare_folders(folder1, folder2):
differences_found = False
# Compare files in folder1
for root, dirs, files in os.walk(folder1):
for file in files:
file1 = os.path.join(root, file)
file2 = os.path.join(root.replace(folder1, folder2, 1), file)
if not os.path.exists(file2):
print(f"File {file} exists in {folder1} but not in {folder2}")
differences_found = True
elif os.path.isfile(file2):
# If the file exists in both folders, compare them
compare_files(file1, file2)
# Compare files in folder2
for root, dirs, files in os.walk(folder2):
for file in files:
file1 = os.path.join(root.replace(folder2, folder1, 1), file)
file2 = os.path.join(root, file)
if not os.path.exists(file1):
print(f"File {file} exists in {folder2} but not in {folder1}")
differences_found = True
if not differences_found:
print("No differences found between the files in the two folders.")
if __name__ == "__main__":
folder1 = input("Enter path to the first folder: ")
folder2 = input("Enter path to the second folder: ")
compare_folders(folder1, folder2)
我想比较两个文件夹里的文件,找出它们之间不同的内容。同时也想列出在两个文件夹中不同的文件名。
1 个回答
0
这段代码有几个问题:
compare_files
现在并没有进行任何比较。它应该先比较两个输入文件的大小,然后再检查内容是否相同。根据文件是否相等,它应该返回 True 或 False,这个返回值应该用来设置differences_found
。- 如果你发现两个文件之间有差异,或者某个文件在一个目录中存在但在另一个目录中不存在,你输出的消息并没有显示文件的完整路径;你只是在输出根文件夹的名字。
- 你写的是:
def compare_folders(folder1, folder2):
differences_found = False
# Compare files in folder1
for root, dirs, files in os.walk(folder1):
for file in files:
file1 = os.path.join(root, file)
file2 = os.path.join(root.replace(folder1, folder2, 1), file)
if not os.path.exists(file2):
print(f"File {file} exists in {folder1} but not in {folder2}")
differences_found = True
elif os.path.isfile(file2):
# If the file exists in both folders, compare them
compare_files(file1, file2)
...
我认为应该是:
def compare_folders(folder1, folder2):
differences_found = False
# Compare files in folder1
for root1, dirs, files in os.walk(folder1):
for file in files:
file1 = os.path.join(root1, file)
root2 = root1.replace(folder1, folder2, 1)
file2 = os.path.join(root2, file)
if not os.path.isfile(file2):
print(f"File {file} exists in {root1} but not in {root2}")
differences_found = True
else:
# If the file exists in both folders, compare them
if not compare_files(file1, file2):
print(f"Files {file} differ in {root1} and {root2}")
differences_found = True
...
这样不仅可以从最初的文件夹开始打印出文件的完整路径,还修正了一个小错误:假设我们有文件夹 folder1/a/b/test,但 folder2/a/b/test 存在,但它是一个目录而不是文件。你现在的代码不会显示文件 test 在一个目录中存在而在另一个目录中不存在,也不会设置 differences_found
标志。
我在下面的完整代码中提供了 compare_files
的实现:
import os
import mmap
def compare_files(file1: str, file2: str) -> bool:
"""Function to compare files."""
with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
size1 = f1.seek(0, 2)
size2 = f2.seek(0, 2)
if size1 != size2:
return False
if size1 == 0:
return True
with mmap.mmap(f1.fileno(), 0, access=mmap.ACCESS_READ) as mm1, \
mmap.mmap(f2.fileno(), 0, access=mmap.ACCESS_READ) as mm2:
return mm1[0:] == mm2[0:]
def compare_folders(folder1: str, folder2: str) -> None:
differences_found = False
# Compare files in folder1
for root1, dirs, files in os.walk(folder1):
for file in files:
file1 = os.path.join(root1, file)
root2 = root1.replace(folder1, folder2, 1)
file2 = os.path.join(root2, file)
if not os.path.isfile(file2):
print(f"File {file} exists in {root1} but not in {root2}")
differences_found = True
else:
# If the file exists in both folders, compare them
if not compare_files(file1, file2):
print(f"Files {file} differ in {root1} and {root2}")
differences_found = True
# Compare files in folder2
for root2, dirs, files in os.walk(folder2):
for file in files:
root1 = root2.replace(folder2, folder1, 1)
file1 = os.path.join(root1, file)
file2 = os.path.join(root2, file)
if not os.path.isfile(file1):
print(f"File {file} exists in {root2} but not in {root1}")
differences_found = True
if not differences_found:
print("No differences found between the files in the two folders.")
if __name__ == "__main__":
folder1 = input("Enter path to the first folder: ")
folder2 = input("Enter path to the second folder: ")
compare_folders(folder1, folder2)
上面的代码对我来说是有效的,所以如果你仍然遇到困难,你需要明确展示出最小的目录结构,这些结构没有产生你期望的结果,同时说明你期望的结果是什么,以及你实际显示的内容。