Implement expression search in dirlist

This commit is contained in:
Dirk Alders 2024-10-11 09:36:36 +02:00
parent c10e8792ab
commit 9237f6f7f7

View File

@ -62,11 +62,12 @@ __INTERPRETER__ = (3, )
"""The Tested Interpreter-Versions""" """The Tested Interpreter-Versions"""
def dirlist(path='.', rekursive=True): def dirlist(path='.', expression='*', rekursive=True):
""" """
Function returning a list of directories below a given path. Function returning a list of directories below a given path.
:param str path: folder which is the basepath for searching files. :param str path: folder which is the basepath for searching files.
:param str expression: expression to fit including shell-style wildcards. It is only used for the first directory level, if rekursive is set to True.
:param bool rekursive: search all subfolders if True. :param bool rekursive: search all subfolders if True.
:returns: list of filenames including the pathe :returns: list of filenames including the pathe
:rtype: list :rtype: list
@ -82,12 +83,11 @@ def dirlist(path='.', rekursive=True):
li = list() li = list()
if os.path.exists(path): if os.path.exists(path):
logger.debug('DIRLIST: path (%s) exists - looking for directories to append', path) logger.debug('DIRLIST: path (%s) exists - looking for directories to append', path)
for dirname in os.listdir(path): for dirname in glob.glob(os.path.join(path, expression)):
fulldir = os.path.join(path, dirname) if os.path.isdir(dirname):
if os.path.isdir(fulldir): li.append(dirname)
li.append(fulldir)
if rekursive: if rekursive:
li.extend(dirlist(fulldir)) li.extend(dirlist(dirname))
else: else:
logger.warning('DIRLIST: path (%s) does not exist - empty filelist will be returned', path) logger.warning('DIRLIST: path (%s) does not exist - empty filelist will be returned', path)
return li return li