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().
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().
Use glob to match pathnames like /home/juser/something/*.txt for all text files within /home/juser directory. You can use it to match simple filenames like *.txt for all text files in the current working directory.
fnmatch.fnmatch and fnmatch.filter are used for filenmaes. The former, tests if a filename matches a pattern and returns True for matched names, otherwise False for unmatched names. The latter, returns the matched filenames according to the giving glob pattern.
All your filenames begin with PRD.FRB.D01.INTRADAY. and end with .txt suffix, then to match all files that begin with PRD.FRB.D01.INTRADAY. and end with .txt irrespective of what's in the middle:
glob.glob("PRD.FRB.D01.INTRADAY.*.txt")
This glob matches any filename that starts with PRD.FRB.D01.INTRADAY. and ends with .txt suffix, it doesn't matter what comes after PRD.FRB.D01.INTRADAY. the * wildcard character matches any arbitrary character. ? matches only one arbitrary character. Note, This matches filenames in the working directory of your script. If you need to match names in a different directory, pass "/path/to/my/PRD.FRB.D01.INTRADAY.*.txt" to glob.
This should work, the asterisk symbol * should also be a string.
import glob
all_files = []
for file in glob.glob('PRD.FRB.D01.INTRADAY.*.' + file_name_date + '.' + h_server + '.txt''):
all_files.append(file)
Glob expression in brackets is a set of characters, not a list of strings.
You first expresion input_stuff = '1,2,3' is equivalent to '123,' and will also match a name starting with comma.
Your second expression contains '-', which is used to denote character ranges like '0-9A-F', hence the error you get.
It is better to drop glob altogether, split input_stuff and use listdir.
import re, os
input_stuff = '1-my-file, 2-my-file, 3-my-file'
folder = '.'
prefixes = re.split(r'\s*,\s*', input_stuff) #split on commas with optional spaces
prefixes = tuple(prefixes) # startswith doesn't work with list
file_names = os.listdir(folder)
filtered_names = [os.path.join(folder, fname) for fname in file_names
if file_name.startswith(prefixes)]
You can use the following:
input_stuff = '1,2,3'
glob(folder+'['+input_stuff+']-my-file*')
EDIT: Since you said in your comment that you can't hardcode "-my-file", you can do something like:
input_stuff = '1,2,3'
name = "-my-file"
print glob.glob(folder+'['+input_stuff+']'+name+'*')
and then just change the "name" variable when you need to.
The brackets in glob are used for character classes (e.g. [a-z] will match lowercase letters). You can put each bracket in a character class to force them being matched:
path1 = "/Users/smcho/Desktop/bracket/[[]10,20[]]"
[[] is a character class containing only the character [, and []] is a character class containing only the character ] (the closing bracket can be placed in a character class by putting it in the first position).
Additionally, since brackets aren't escaped in string literals, your code will look for a backslash as well as a bracket.
In Python 3.4 you can use glob.escape.
The pattern rules for glob are not regular expressions. Instead, they follow standard Unix path expansion rules. There are only a few special characters: two different wild-cards, and character ranges are supported [from pymotw: glob โ Filename pattern matching].
So you can exclude some files with patterns.
For example to exclude manifests files (files starting with _) with glob, you can use:
files = glob.glob('files_path/[!_]*')
You can deduct sets and cast it back as a list:
list(set(glob("*")) - set(glob("eph*")))