![](/img/trans.png)
[英]python - comparing a newly written file with filecmp.cmp() always returns False?
[英]Comparing file with a list of files using filecmp.cmp(file1, file2)
我有一個帶有三個參數的 function
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_file
與list_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.