[英]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.