使用filecmp.cmp(file1,file2)将文件与文件列表进行比较



我有一个带有三个参数的函数

  • list_to_copy:要复制的文件列表
  • list_to_avoid:我不想复制的列表,可能存在于list_to_copy中
  • 目的地是文件所在的路径
def copy_files(self, list_to_copy, list_to_avoid, destination):
# Copy a file from list to destination making sure file is not 
# duplicated regarding the name.
for copied_file in list_to_copy:
for avoid_file in list_to_avoid:
if not filecmp.cmp(copied_file, avoid_file):
shutil.copy(copied_file, destination)

我的主要问题是我不知道如何使用filecmp.cmp(file1, file2)copied_filelist_to_avoid中的所有文件进行比较


按文件名(字符串(进行注释检查对我来说效率不高。

from filecmp import cmp
from shutil import copy
from os import scandir
def copy_files(list_to_copy,destination):
# Copy a file from list to destination making sure file is not duplicated regarding the content.
for copied_file in list_to_copy:
for avoid_file in scandir(destination):
if cmp(copied_file,avoid_file):
break
else:
copy(copied_file, destination)

最新更新