I am trying to delete all duplicate files from my computer using a Python script
import os
import hashlib
# Set the directory you want to search for duplicate files
dir_path = "/Users/ebbyrandall"
# Create a dictionary to store the files and their corresponding hashes
files_dict = {}
# Walk through the directory and calculate the hash of each file
for root, dirs, files in os.walk(dir_path):
for file in files:
file_path = os.path.join(root, file)
with open(file_path, "rb") as f:
file_data = f.read()
file_hash = hashlib.md5(file_data).hexdigest()
# If the file hash is not in the dictionary, add it
# Otherwise, it's a duplicate and we should delete it
if file_hash not in files_dict:
files_dict[file_hash] = file_path
else:
os.remove(file_path)
I get the following error message:
Traceback (most recent call last):
File "deleteDuplicateFiles.py", line 14, in <module>
with open(file_path, "rb") as f:
IOError: [Errno 2] No such file or directory: '/Users/ebbyrandall/myPython/env/bin/python3'