I haven't used regex a lot and I needed to set up a script that can gather a list of file paths that should adhere to a strict formatting convention, so I thought that sounded like a good opportunity to use them.
To explain a bit, there's a set of sequence folders inside a root folder, and in those sequences is a set of scene folders. In those is a set of constant named folders, the relevant one being "AEP Files". Then in there is a set of after effects files, of which I want to get the highest numbered version which is denoted but
_v##.aep at the end of the file. A sample path might look like this:
I'm particularly interested to know
- If I'm using regex correctly,
- Whether I should use something other than
- Whether I could make it more efficient (particularly the list comprehensions); and
- Given the complexity, how is the current readability?
I had ideas on more efficiency but they involved collapsing list comprehensions down and I thought that would make an unreadable mess, so I opted not to. Any other feedback you want to give is also welcome!
import os import re root = r'P:\ProjectName\Scenes' IGNORE = re.IGNORECASE folders = [os.path.join(root, f) for f in os.listdir(root) if re.match(r'e\d*q\d*', f, IGNORE)] folders.sort() scene_folders = [os.path.join(folder, f) for folder in folders for f in os.listdir(folder) if re.match(r'e\d*_q\d*_s\d*', f, IGNORE)] scene_folders.sort() scenes =  missing_scenes =  for folder in scene_folders: matches = [re.match(r'proj_q\d*_s\d*_v(\d*)\.aep', f, IGNORE) for f in os.listdir(os.path.join(folder, "AEP Files")) if re.match(r'proj_q\d*_s\d*_v(\d*)\.aep', f, IGNORE)] matches = [(match.group(), match.groups()) for match in matches] if matches: scenes.append(os.path.join(folder, sorted(matches)[-1])) else: missing_scenes.append(folder)