假设您有一个包含成百上千个.csv
或.txt
文件的文件夹,这些文件可能包含不同的信息,但是您要确保joe041.txt
实际上不包含相同的信息意外地为joe526.txt
的数据。
我已经习惯于使用Python脚本本质上读取目录中的每个文件并计算一个校验和,然后将其比较,而不是将所有内容都加载到一个文件中(如果每个文件都有数千行,这可能会很麻烦)。在数千个文件之间。
有没有更有效的方法?
即使使用filecmp
似乎效率也较低,因为该模块仅具有 file vs file 和 dir vs dir 比较,而没有 file vs dir 命令–这意味着要使用它,您必须迭代 x ²次(dir
中的所有文件与dir
中的所有其他文件)
import os
import hashlib
outputfile = []
for x in(os.listdir("D:/Testing/New folder")):
with open("D:/Testing/New folder/%s" % x, "rb") as openfile:
text=openfile.read()
outputfile.append(x)
outputfile.append(",")
outputfile.append(hashlib.md5(text).hexdigest())
outputfile.append("\n")
print(outputfile)
with open("D:/Testing/New folder/output.csv","w") as openfile:
for x in outputfile:
openfile.write(x)
答案 0 :(得分:4)
受@sɐunıɔןɐqɐp注释的启发,您可以尝试一种迭代方法,该方法首先对所有文件执行廉价操作(获取文件大小),然后对具有相同大小的那些文件进行更深入的比较。
此代码首先比较文件的大小,然后比较文件的第一行,最后比较整个文件的md5
哈希。您可以根据自己的使用情况对其进行调整。
我使用长的变量名使其明确;不要为此而分心。
import os
import hashlib
def calc_md5(file_path):
hash_md5 = hashlib.md5()
with open(file_path, 'rb') as f:
for chunk in iter(lambda: f.read(4096), b''):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def get_duplicates_by_size(dir_path):
files_by_size = {}
for elem in os.listdir(dir_path):
file_path = os.path.join(dir_path, elem)
if os.path.isfile(file_path):
size = os.stat(file_path).st_size
if size not in files_by_size:
files_by_size[size] = []
files_by_size[size].append(file_path)
# keep only entries with more than one file;
# the others don't need to be kept in memory
return {
size: file_list
for size, file_list in files_by_size.items()
if len(file_list) > 1}
def get_duplicates_by_first_content(files_by_size, n_chars):
files_by_size_and_first_content = {}
for size, file_list in files_by_size.items():
d = {}
for file_path in file_list:
with open(file_path) as f:
first_content = f.read(n_chars)
if first_content not in d:
d[first_content] = []
d[first_content].append(file_path)
# keep only entries with more than one file;
# the others don't need to be kept in memory
d = {
(size, first_content): file_list_2
for first_content, file_list_2 in d.items()
if len(file_list_2) > 1}
files_by_size_and_first_content.update(d)
return files_by_size_and_first_content
def get_duplicates_by_hash(files_by_size_and_first_content):
files_by_size_and_first_content_and_hash = {}
for (size, first_content), file_list in files_by_size_and_first_content.items():
d = {}
for file_path in file_list:
file_hash = calc_md5(file_path)
if file_hash not in d:
d[file_hash] = []
d[file_hash].append(file_path)
# keep only entries with more than one file;
# the others don't need to be kept in memory
d = {
(size, first_content, file_hash): file_list_2
for file_hash, file_list_2 in d.items()
if len(file_list_2) > 1}
files_by_size_and_first_content_and_hash.update(d)
return files_by_size_and_first_content_and_hash
if __name__ == '__main__':
r = get_duplicates_by_size('D:/Testing/New folder')
r = get_duplicates_by_first_content(r, 20) # customize the number of chars to read
r = get_duplicates_by_hash(r)
for k, v in r.items():
print('Key:', k)
print(' Files:', v)