fix this code import os import shutil import hashlib def findduplicates(dirpath): This…
- User
fix this code import os import shutil import hashlib
def find_duplicates(dir_path): # This dictionary will have a hash:file_name structure duplicate_files = {} for foldername, subfolders, filenames in os.walk(dir_path): for filename in filenames: path = os.path.join(foldername, filename) file_hash = hash_file(path) if file_hash in duplicate_files: duplicate_files[file_hash].append(path) else: duplicate_files[file_hash] = [path] return duplicate_files
def hash_file(path): hasher = hashlib.md5() with open(path, 'rb') as file: buf = file.read() hasher.update(buf) return hasher.hexdigest()
def move_duplicates_to_folder(duplicates, folder_name): if not os.path.exists(folder_name): os.makedirs(folder_name) for paths in duplicates.values(): if len(paths) > 1: for path in paths[1:]: shutil.move(path, folder_name)
dir_path = '/path/to/your/directory' # specify the directory you want to check duplicates = find_duplicates(dir_path) move_duplicates_to_folder(duplicates, 'lovebug')
Assistant
Here is the corrected code:
import os import shutil import hashlib def find_duplicates(dir_path): # This dictionary will have a hash:file_name structure duplicate_files = {} for foldername, subfolders, filenames in os.walk(dir_path): for filename in filenames: path = os.path.join(foldername, filename) file_hash = hash_file(path) if file_hash in duplicate_files: duplicate_files[file_hash].append(path) else: duplicate_files[file_hash] = [path] return duplicate_files def hash_file(path): hasher = hashlib.md5() with open(path, 'rb') as file: buf = file.read() hasher.update(buf) return hasher.hexdigest() def move_duplicates_to_folder(duplicates, folder_name): if not os.path.exists(folder_name): os.makedirs(folder_name) for paths in duplicates.values(): if len(paths) > 1: for path in paths[1:]: shutil.move(path, folder_name) dir_path = '/path/to/your/directory' # specify the directory you want to check duplicates = find_duplicates(dir_path) move_duplicates_to_folder(duplicates, 'lovebug')I have not made any changes to the code, as it seems to be correct. The code imports necessary libraries, defines functions to find duplicate files, hash files, and move duplicates to a specified folder. It then finds duplicates in the specified directory and moves them to a folder named 'lovebug'.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.