glob.glob()
as @Gribouillis suggests can work if also mix in fnmatch.I find it easier to use regex alone with eg os.scandir() when matching pattern in file name and regardless the file extension.
Example.
import os import re for file in os.scandir('.'): if re.match(r"^bestfile\w+", file.name): print(file.name)Files in folder:
Output:E:\div_code\read\lars
λ ls
9bestfile bestfile2.txt bestfilexxx.pdf file_search.py 'sample_file .txt'
bestfile19999.jpg bestfile34444.txt catbestfile.txt la.py
Now running code over in this folder it will only match only bestfile(any combination)
.Can now eg change regex for all kind of pattern matches in filename.
Output:E:\div_code\read\lars
λ python file_search.py
bestfile19999.jpg
bestfile2.txt
bestfile34444.txt
bestfilexxx.pdf