简体   繁体   中英

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

I have a function with three args

  • list_to_copy: list of files that I want to copy.
  • list_to_avoid: list that I don't want to copy, that might exists in list_to_copy
  • the destination is the path where the file is going to be.
    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)

My main problem is I don't know how to compare copied_file with all files in list_to_avoid using filecmp.cmp(file1, file2)


Note checking by name of the file (string) isn't efficient for me.

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)

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.

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