- Apr 28, 2021
- 1,004
- 1,279
[/SPOILER]Python:import os import hashlib import shutil def get_file_hash(file_path): """Generate a hash for a given file.""" hasher = hashlib.md5() with open(file_path, 'rb') as f: for chunk in iter(lambda: f.read(4096), b''): hasher.update(chunk) return hasher.hexdigest() def reduce_cache_duplicates(directory): """Reduce duplicate files in the cache directory.""" # Create a dictionary to store file hashes as keys and file paths as values file_hash_dict = {} # Iterate over all files in the directory and its subdirectories for root, dirs, files in os.walk(directory): for file in files: file_path = os.path.join(root, file) file_hash = get_file_hash(file_path) # If the hash is already in the dictionary, it means the file is a duplicate # Create a reference to the existing file instead of keeping the duplicate if file_hash in file_hash_dict: reference_file = file_hash_dict[file_hash] print(f"Removing duplicate file: {file_path}") os.remove(file_path) os.symlink(reference_file, file_path) print(f"Created symlink for {file_path} pointing to {reference_file}") else: file_hash_dict[file_hash] = file_path print(f"Processed file: {file_path}") # Get the current directory current_directory = os.getcwd() # Call the function to reduce duplicates in the current directory and its subdirectories print("Reducing duplicate files...") reduce_cache_duplicates(current_directory) print("Duplicate reduction complete.")
[/SPOILER]Python:... # Iterate over all files in the directory and its subdirectories for root, dirs, files in os.walk(directory): for file in files: if file.lower().endswith(('vamcachemeta', 'vamcachejson')): continue file_path = os.path.join(root, file) file_hash = get_file_hash(file_path) ...
