I want to check whether file names in a directory correspond to a regex pattern, that works. Now it would be nice to actually mark the error, so the user knows what to fix. Is there a way to do this with re directly (preferable using match()
or fullmatch()
) so that I don't have to write ugly code with 20 different conditions?
This is my code.
def checkname():
dir_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..",".."))
for root, dirs, files in os.walk(dir_path):
for file in files:
if re.match(r".+\.(mei|mscz|mxl)$",file) and not re.match(r"PL_WRk_352_\d+(r|v)(-\d+(r|v))?_[^\W\d]+_n\d+_enc_dipl_CMN",file):
print(f"wrong file: {os.path.join(root, file)}")
It correctly prints out all the wrong files buit this is not too useful since the filenames are complex and a lot of mistakes can happen. So it would be great to now where re.match(r"PL_WRk_352_\d+(r|v)(-\d+(r|v))?_[^\W\d]+_n\d+_enc_dipl_CMN",file)
failed to show the user at least the first error. Is this possible?