diff --git a/src/imcflibs/imagej/bdv.py b/src/imcflibs/imagej/bdv.py index cfcb12a..f9fb4db 100644 --- a/src/imcflibs/imagej/bdv.py +++ b/src/imcflibs/imagej/bdv.py @@ -36,7 +36,7 @@ def backup_xml_files(source_directory, subfolder_name): pathtools.create_directory(xml_backup_directory) backup_subfolder = xml_backup_directory + "/%s" % (subfolder_name) pathtools.create_directory(backup_subfolder) - all_xml_files = pathtools.listdir_matching(source_directory, ".xml*") + all_xml_files = pathtools.listdir_matching(source_directory, ".xml*", regex=True) os.chdir(source_directory) for xml_file in all_xml_files: shutil.copy2(xml_file, backup_subfolder) diff --git a/src/imcflibs/pathtools.py b/src/imcflibs/pathtools.py index 37839e6..832ae39 100644 --- a/src/imcflibs/pathtools.py +++ b/src/imcflibs/pathtools.py @@ -3,13 +3,14 @@ import os.path import platform from os import sep +import re from . import strtools from .log import LOG as log def parse_path(path, prefix=""): - r"""Parse a path into its components. + """Parse a path into its components. If the path doesn't end with the pathsep, it is assumed being a file! No tests based on existing files are done, as this is supposed to also work @@ -156,7 +157,7 @@ def jython_fiji_exists(path): return False -def listdir_matching(path, suffix, fullpath=False, sort=False): +def listdir_matching(path, suffix, fullpath=False, sort=False, regex=False): """Get a list of files in a directory matching a given suffix. Parameters @@ -180,12 +181,17 @@ def listdir_matching(path, suffix, fullpath=False, sort=False): """ matching_files = list() for candidate in os.listdir(path): - if candidate.lower().endswith(suffix.lower()): + if not regex and candidate.lower().endswith(suffix.lower()): # log.debug("Found file %s", candidate) if fullpath: matching_files.append(os.path.join(path, candidate)) else: matching_files.append(candidate) + if regex and re.match(suffix.lower(), candidate.lower()): + if fullpath: + matching_files.append(os.path.join(path, candidate)) + else: + matching_files.append(candidate) if sort: matching_files = strtools.sort_alphanumerically(matching_files)