From e7562054d06e2482f274359d7dd9904d3c871c55 Mon Sep 17 00:00:00 2001 From: Auroir <36361058+Auroir@users.noreply.github.com> Date: Sat, 17 Aug 2019 21:01:44 -0700 Subject: [PATCH] Fix sorting pathlib objects (#353) Fixing an issue caused by attempting to sort Path objects. Directly using `<` is unsupported between these, so `sorted()` needs a key specified. "PurePath" objects support `>` while normal paths do not, causing the confusion. https://docs.python.org/3/library/pathlib.html --- utils/Path_utils.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/utils/Path_utils.py b/utils/Path_utils.py index d34e348..e753834 100644 --- a/utils/Path_utils.py +++ b/utils/Path_utils.py @@ -31,19 +31,18 @@ def get_image_unique_filestem_paths(dir_path, verbose_print_func=None): def get_file_paths(dir_path): dir_path = Path (dir_path) - result = [] if dir_path.exists(): - return [ x.path for x in list(scandir(str(dir_path))) if x.is_file() ] - return sorted(result) + return sorted([ x.path for x in list(scandir(str(dir_path))) if x.is_file() ]) + else: + return [] def get_all_dir_names (dir_path): dir_path = Path (dir_path) - result = [] if dir_path.exists(): - return [ x.name for x in list(scandir(str(dir_path))) if x.is_dir() ] - - return sorted(result) + return sorted([ x.name for x in list(scandir(str(dir_path))) if x.is_dir() ]) + else: + return [] def get_all_dir_names_startswith (dir_path, startswith): dir_path = Path (dir_path) @@ -61,7 +60,7 @@ def get_first_file_by_stem (dir_path, stem, exts=None): stem = stem.lower() if dir_path.exists(): - for x in sorted(list(scandir(str(dir_path)))): + for x in sorted(list(scandir(str(dir_path))), key=lambda x: x.name): if not x.is_file(): continue xp = Path(x.path)