I have 4 files inside a folder hash.py size_1.py size_2.py size.py
. size_1.py , size_2.py is of same size.
I am creating hashlib for the files
import hashlib, os, sys
result = {}
for root, dirs,files in os.walk(".", topdown=True):
for name in files:
#print(os.path.join(root, name))
FileName = (os.path.join(root, name))
hasher = hashlib.md5()
with open(str(FileName), 'rb') as afile:
buf = afile.read()
hasher.update(buf)
file_hash = (afile,hasher.hexdigest())
#print (file_hash)
result[file_hash[1]] = file_hash[0]
#if file_hash[1] in result:
# result[file_hash[1]].append(file_hash[0])
#else:
# result[file_hash[1]] = file_hash[0]
print (result)
My Output
{'e12d780eba6e03a7c1cafa394ef9f31f': <_io.BufferedReader name='./size.py'>, '49eb7137273ec333727ea0f5279fe040': <_io.BufferedReader name='./size_1.py'>, '35e93b380f084d5187976beae746492e': <_io.BufferedReader name='./hash.py'>}
My Desired Out
{'e12d780eba6e03a7c1cafa394ef9f31f': ['./size.py']>, '49eb7137273ec333727ea0f5279fe040': ['./size_1.py','./size_1.py'], '35e93b380f084d5187976beae746492e': ['./hash.py']}
Here 2 uplift has to do
remove <_io.BufferedReader name=
put it in a dictionary format.
I'm not sure why are you create tuples where they don't required.
import hashlib, os, sys
result = {}
for root, dirs, files in os.walk('.', topdown=True):
for name in files:
hasher = hashlib.md5()
fn = os.path.join(root, name)
with open(fn, 'rb') as afile:
buf = afile.read()
hasher.update(buf)
file_hash = hasher.hexdigest()
if fn in result:
result[fn].append(file_hash)
else:
result[fn] = [file_hash]