簡體   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