I'm having issues with this code "@dataclass class FileInfo: filepath: str hashvalue:…
- User
I'm having issues with this code "@dataclass class FileInfo: filepath: str hash_value: str atime: float ctime: float mtime: float
def hash_file(file_path): """Calculate the MD5 hash of a file.""" hasher = hashlib.md5() try: with open(file_path, 'rb') as f: buf = f.read() hasher.update(buf) return hasher.hexdigest(), None except Exception as e: return None, f"Error hashing file {file_path}: {e}"
def hash_file_wrapper(file_path): hash_value, error = hash_file(file_path) return file_path, hash_value, error
def find_files(directory): """Find files of specified types in the given directory and hash them.""" files = [] errors = [] most_recent_file = None most_recent_mtime = 0
codewith concurrent.futures.ProcessPoolExecutor() as executor: futures = [] for root, _, files_list in os.walk(directory): for file_name in files_list: file_path = os.path.join(root, file_name) if os.path.isfile(file_path) and os.path.splitext(file_name)[1] in file_types: future = executor.submit(hash_file_wrapper, file_path) futures.append(future) for future in tqdm(concurrent.futures.as_completed(futures), total=len(futures), desc="Hashing files"): file_path, hash_value, error = future.result() if hash_value: atime = os.path.getatime(file_path) ctime = os.path.getctime(file_path) mtime = os.path.getmtime(file_path) files.append(FileInfo(filepath=file_path, hash_value=hash_value, atime=atime, ctime=ctime, mtime=mtime)) if mtime > most_recent_mtime: most_recent_mtime = mtime most_recent_file = file_path elif error: errors.append(error) return files, most_recent_file, errorsdef find_duplicates(paths, most_recent_file): """Find duplicate files in the given directories.""" duplicates = {}
code - Assistant
There are several potential issues that could be causing your script to only output one line in the Excel file when it should have hundreds of rows. I'll walk you through a list of checks you can make to find out why it's not functioning as expected.
- User
could you give me a fix for the code
- Assistant