假设您有一个文件夹,其中包含可能包含不同信息的成百上千个.csv
或.txt
文件,但您想确保它joe041.txt
实际上不包含joe526.txt
意外的相同数据。
与其将所有内容都加载到一个文件中(如果每个文件都有数千行,这可能会很麻烦),我已经开始使用 Python 脚本来基本上读取目录中的每个文件并计算校验和,然后您可以在数千行之间进行比较的文件。
有没有更有效的方法来做到这一点?
即使使用filecmp
了,这似乎不太有效,因为模块只有文件VS文件和目录VS目录比较,但没有文件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)
Run Code Online (Sandbox Code Playgroud)