From b6f9479bf7275a23b1c13ce2dfb63f3fa467c6b9 Mon Sep 17 00:00:00 2001 From: "Antonio J. Delgado" Date: Sat, 28 Jan 2023 10:38:55 +0200 Subject: [PATCH] add check for existing path --- find_duplicate_files/find_duplicate_files.py | 57 ++++++++++---------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/find_duplicate_files/find_duplicate_files.py b/find_duplicate_files/find_duplicate_files.py index edd838d..fd246b2 100644 --- a/find_duplicate_files/find_duplicate_files.py +++ b/find_duplicate_files/find_duplicate_files.py @@ -103,33 +103,36 @@ class find_duplicate_files: def recursive_scandir(self, path, ignore_hidden_files=True): ''' Recursively scan a directory for files''' files = dict() - try: - for file in os.scandir(path): - if self.limit > 0 and len(files) > self.limit: - self._log.debug(f"# Limit of {self.limit} passed ({len(files)})") - break - if not file.name.startswith('.'): - if not self._test_exclude(file.path): - if file.is_file(): - check_cache = self._check_file_cache(file.path) - if check_cache: - files[check_cache] = file.path - else: - with open(file.path, 'rb') as file_pointer: - file_content = file_pointer.read() - hash = zlib.adler32(file_content) - files[hash] = file.path - self._cache_file(file.path, hash) - elif file.is_dir(follow_symlinks=False): - more_files = self.recursive_scandir( - file.path, - ignore_hidden_files=ignore_hidden_files - ) - if more_files: - files = { **files, **more_files } - except PermissionError as error: - self._log.warning(f"# Permission denied accessing folder '{path}'") - self._log.debug(f"# Found {len(files)} files in '{path}'. Cache contains {self._cache_size()} records.") + if os.path.exists(path): + try: + for file in os.scandir(path): + if self.limit > 0 and len(files) > self.limit: + self._log.debug(f"# Limit of {self.limit} passed ({len(files)})") + break + if not file.name.startswith('.'): + if not self._test_exclude(file.path): + if file.is_file(): + check_cache = self._check_file_cache(file.path) + if check_cache: + files[check_cache] = file.path + else: + with open(file.path, 'rb') as file_pointer: + file_content = file_pointer.read() + hash = zlib.adler32(file_content) + files[hash] = file.path + self._cache_file(file.path, hash) + elif file.is_dir(follow_symlinks=False): + more_files = self.recursive_scandir( + file.path, + ignore_hidden_files=ignore_hidden_files + ) + if more_files: + files = { **files, **more_files } + except PermissionError as error: + self._log.warning(f"# Permission denied accessing folder '{path}'") + self._log.debug(f"# Found {len(files)} files in '{path}'. Cache contains {self._cache_size()} records.") + else: + self._log.warning(f"# Folder '{path}' doesn't exist") return files def _init_log(self):