Skip to content

Commit

Permalink
Add regex as input for the listdir_matching method
Browse files Browse the repository at this point in the history
  • Loading branch information
lguerard committed Sep 20, 2023
1 parent a219b0b commit 4a60fd1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/imcflibs/imagej/bdv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions src/imcflibs/pathtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 4a60fd1

Please sign in to comment.