I want to make a list of files with extend()
while ignoring any file name ending with 0 as in _file0.h5
. I have this line for concatenating all the files into a list
data_files_0.extend(sorted(glob(f"{directory}*_file{filenum}.h5") ) )
I am trying to learn how to implement regex here and I tried
filenum = re.match(r'[^0]')
by putting it above the previous line which gives the error
TypeError: match() missing 1 required positional argument: 'string'
I am pretty confused here and all the examples of f-string with regex don't help me at all.
re.match
won't automatically look for strings containing that pattern - you have to provide the string to it - that's the cause of the missing argument error.
Example - re.match('[^0]', "abc0123")
will check for matches in the string "abc0123" according to the pattern '[^0]'.
[^0]
is likely wrong pattern here since this matches any and every character at any position in the string except for a 0
. You might want to use something like .*0\.h5
, which matches any string ending with '0.h5'
. You can also check out regexr.com, which is a very helpful site for figuring out how regex patterns work in general.
For the other part of the problem - finding the files - you might have to just get all the filenames first, then check which ones end with 0 or not:
all_files = glob(f"{directory}*_file*.h5")
for f in all_files:
if not re.match('.*0\.h5', f):
data_files_0.append(f)