add check for existing path

This commit is contained in:
Antonio J. Delgado 2023-01-28 10:38:55 +02:00
parent 226d5e9bb2
commit b6f9479bf7

View file

@ -103,33 +103,36 @@ class find_duplicate_files:
def recursive_scandir(self, path, ignore_hidden_files=True): def recursive_scandir(self, path, ignore_hidden_files=True):
''' Recursively scan a directory for files''' ''' Recursively scan a directory for files'''
files = dict() files = dict()
try: if os.path.exists(path):
for file in os.scandir(path): try:
if self.limit > 0 and len(files) > self.limit: for file in os.scandir(path):
self._log.debug(f"# Limit of {self.limit} passed ({len(files)})") if self.limit > 0 and len(files) > self.limit:
break self._log.debug(f"# Limit of {self.limit} passed ({len(files)})")
if not file.name.startswith('.'): break
if not self._test_exclude(file.path): if not file.name.startswith('.'):
if file.is_file(): if not self._test_exclude(file.path):
check_cache = self._check_file_cache(file.path) if file.is_file():
if check_cache: check_cache = self._check_file_cache(file.path)
files[check_cache] = file.path if check_cache:
else: files[check_cache] = file.path
with open(file.path, 'rb') as file_pointer: else:
file_content = file_pointer.read() with open(file.path, 'rb') as file_pointer:
hash = zlib.adler32(file_content) file_content = file_pointer.read()
files[hash] = file.path hash = zlib.adler32(file_content)
self._cache_file(file.path, hash) files[hash] = file.path
elif file.is_dir(follow_symlinks=False): self._cache_file(file.path, hash)
more_files = self.recursive_scandir( elif file.is_dir(follow_symlinks=False):
file.path, more_files = self.recursive_scandir(
ignore_hidden_files=ignore_hidden_files file.path,
) ignore_hidden_files=ignore_hidden_files
if more_files: )
files = { **files, **more_files } if more_files:
except PermissionError as error: files = { **files, **more_files }
self._log.warning(f"# Permission denied accessing folder '{path}'") except PermissionError as error:
self._log.debug(f"# Found {len(files)} files in '{path}'. Cache contains {self._cache_size()} records.") 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 return files
def _init_log(self): def _init_log(self):