繁体   English   中英

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

[英]Comparing file with a list of files using filecmp.cmp(file1, file2)

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

  • 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM