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"""
def dirlist(path='.', rekursive=True):
def dirlist(path='.', expression='*', rekursive=True):
"""
Function returning a list of directories below a given path.
: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.
:returns: list of filenames including the pathe
:rtype: list
@ -82,12 +83,11 @@ def dirlist(path='.', rekursive=True):
li = list()
if os.path.exists(path):
logger.debug('DIRLIST: path (%s) exists - looking for directories to append', path)
for dirname in os.listdir(path):
fulldir = os.path.join(path, dirname)
if os.path.isdir(fulldir):
li.append(fulldir)
for dirname in glob.glob(os.path.join(path, expression)):
if os.path.isdir(dirname):
li.append(dirname)
if rekursive:
li.extend(dirlist(fulldir))
li.extend(dirlist(dirname))
else:
logger.warning('DIRLIST: path (%s) does not exist - empty filelist will be returned', path)
return li