2👍
✅
I wouldn’t use regexps for this; it’s best to avoid them when you don’t really need them. Here’s how I’d do it (untested):
def _get_timestamp(self):
"""Return the timestamp of a user's most recently uploaded avatar."""
path = settings.USER_AVATAR_DIRECTORY + self._get_dir()
filenames = [filename for filename in os.listdir(path)
if filename.partition('_')[0] == str(self.user_id)]
filenames.sort(reverse=True)
return (filenames[0].rpartition('_')[2].partition('.')[0]
if filenames else None)
Source:stackexchange.com