Well, you've done most of the work in the assignment, so kudos to you for that. You just need to refine a few things and use your functions together.
For item "a) Create a list of all files": In the for fileName in files:
loop, add the line fileList.append(fullPath)
. (See list.append()
for more info.)
- indent it so it's part of the
for
loop.
- Btw, the
print(files)
line you have is outside the loop so it will only print the files of the last folder that was os.walk
'ed.
- Change that to
print(fileList)
For "c) Iterate through the list of files and...":
Iterate through the fileList
and call the HashFile()
function for each file. The return value is the key for your dictionary and the filepath is the value:
for filepath in fileList:
filehash = HashFile(filepath)
fileHashes[filehash] = filepath
The one-line version of that, using a dictionary comprehension:
fileHashes = {HashFile(filepath): filepath for filepath in fileList}
For "d) Iterate through the dictionary": I think you'll be able to manage that on your own. (See dict.items()
for more info.)
Other notes:
A.) In the except
block for calculating hashes, returning a string of the error - was the pre-written for you in the assignment or did you write it? If you wrote it, consider removing it - coz then that error message becomes the hash of the file since you're returning it. Possibly just print the error; unless you were instructed otherwise.
B.) The "input filename" part is not needed - you'll be calculating the hash of the all files in the "current" directory where your script is executed (as shown above).
C.) Btw, for md5, since finding collisions is a known flaw, may want to add each file as a list of values. Maybe it's part of the test. Or can be safely ignored.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…