You can't use (...) grouping, no. The glob() module uses the fnmatch module to do the matching, and it supports *, ?, [seq] and [!seq], nothing more.

However, fnmatch uses a simple pattern-to-regex conversion to test filenames. Just do the same yourself with os.scandir():

import re
import os

pattern = re.compile("MM05_[a-z0-9]*(_Student[A-Z])?\.py")

for entry in os.scandir("MM05_submissions"):
    if pattern.fullmatch(entry.name):
        # name matched the pattern

If you need to support arbitrary depth patterns using directory names, you'll have to write something up using os.walk().

Answer from Martijn Pieters on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ glob.html
glob โ€” Unix style pathname pattern expansion โ€” Python 3.15.2 documentation
The glob module finds pathnames using pattern matching rules similar to the Unix shell. No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched.

You can't use (...) grouping, no. The glob() module uses the fnmatch module to do the matching, and it supports *, ?, [seq] and [!seq], nothing more.

However, fnmatch uses a simple pattern-to-regex conversion to test filenames. Just do the same yourself with os.scandir():

import re
import os

pattern = re.compile("MM05_[a-z0-9]*(_Student[A-Z])?\.py")

for entry in os.scandir("MM05_submissions"):
    if pattern.fullmatch(entry.name):
        # name matched the pattern

If you need to support arbitrary depth patterns using directory names, you'll have to write something up using os.walk().

Answer from Martijn Pieters on Stack Overflow
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 2 โ€บ glob
glob โ€“ Filename pattern matching - Python Module of the Week
There are only a few special characters: two different wild-cards, and character ranges are supported. The patterns rules are applied to segments of the filename (stopping at the path separator, /). Paths in the pattern can be relative or absolute. Shell variable names and tilde (~) are not expanded. The examples below assume the following test files are present in the current working directory: $ python glob_maketestdata.py dir dir/file.txt dir/file1.txt dir/file2.txt dir/filea.txt dir/fileb.txt dir/subdir dir/subdir/subfile.txt
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Glob_(programming)
glob (programming) - Wikipedia
4 days ago - This option came from ksh93. The GNU fnmatch and glob has an identical extension. globstar: allows ** on its own as a name component to recursively match any number of layers of non-hidden directories. Also supported by the JavaScript libraries and Python's glob.
๐ŸŒ
Python Packaging
packaging.python.org โ€บ en โ€บ latest โ€บ specifications โ€บ glob-patterns
glob patterns - Python Packaging User Guide
For PyPA purposes, a valid glob pattern MUST be a string matched against filesystem entries as specified below: Alphanumeric characters, spaces (`` ), underscores (``_), hyphens (-), and dots (.) MUST be matched verbatim.
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-glob-glob-function
Python glob.glob() Function - Learn By Example
April 14, 2024 - The glob module in Python includes a function glob.glob(), which is used to retrieve files matching a specified pattern, eliminating the need for manual filtering of directory listing results. The pattern matching is based on the rules of Unix globbing (wildcards such as *, ?, [characters], and so on), which are similar to regular expressions but much simpler.
๐ŸŒ
GitHub
facelessuser.github.io โ€บ wcmatch โ€บ glob
wcmatch.glob - Wildcard Match Documentation
glob takes a pattern (or list of patterns), flags, and an optional root directory (string or path-like object) and/or directory file descriptor. It also allows configuring the max pattern limit. Exclusion patterns can be specified via the exclude parameter which takes a pattern or a list of patterns.When executed it will crawl the file system returning matching files. ... Path-like object input support is only available in Python ...
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ python โ€บ glob module
Python | Glob Module | Codecademy
June 25, 2022 - In order to find a literal match of a special character, it must be wrapped in square brackets (e.g. '[*]'). ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python glob: filename pattern matching
Python Glob: Filename Pattern Matching โ€“ PYnative
June 17, 2021 - Using the glob module we can search for exact file names or even specify part of it using the patterns created using wildcard characters. These patterns are similar to regular expressions but much simpler. ... We can specify a range of alphanumeric characters inside the []. We need to import Pythonโ€™s built-in glob module to use the glob() function.
๐ŸŒ
Python Module of the Week
pymotw.com โ€บ 3 โ€บ glob
glob โ€” Filename Pattern Matching โ€” PyMOTW 3
December 28, 2016 - There are only a few special characters used to implement two different wild-cards and character ranges. The pattern rules are applied to segments of the filename (stopping at the path separator, /). Paths in the pattern can be relative or absolute. Shell variable names and tilde (~) are not expanded. The examples in this section assume the following test files are present in the current working directory. $ python3 glob_maketestdata.py dir dir/file.txt dir/file1.txt dir/file2.txt dir/filea.txt dir/fileb.txt dir/file?.txt dir/file*.txt dir/file[.txt dir/subdir dir/subdir/subfile.txt
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
How to Use glob() in Python | note.nkmk.me
July 28, 2023 - In Python, the glob module allows you to get a list or an iterator of file and directory paths that satisfy certain conditions using special characters like the wildcard *. glob โ€” Unix style pathname ...
๐ŸŒ
Medium
martinxpn.medium.com โ€บ glob-working-with-files-in-python-64-100-days-of-python-31e0916f046e
Glob โ€” Working with Files in Python (64/100 Days of Python) | by Martin Mirakyan | Medium
April 10, 2023 - You can also use the ? wildcard character to match a single character. For example, to get a list of all files in the my_folder directory that have a file name with 5 characters followed by a .txt extension, you can use the following code: import glob files = glob.glob('my_folder/?????.txt') print(files)
๐ŸŒ
Malik Browne
malikbrowne.com โ€บ blog โ€บ a-beginners-guide-glob-patterns
A Beginner's Guide: Glob Patterns | Malik Browne
November 11, 2019 - Now that we know a little bit about the history of globs, let's get into the part that makes it useful: wildcard matching. A string can be considered a wildcard pattern if it contains one of the following characters: *, ?, or [.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ glob() in python
glob() in Python - Scaler Topics
May 4, 2023 - Only two wildcards, or special characters, are permitted inside the pattern: "*," and "?" The usual characters that can be stated inside the pattern are expressed in []. The filename segment (which is provided in the functions) is subject to the rules of the pattern for glob module functions, ...