Verifying patterns of various filenames I have a large number of images that were named in a odd way and because of that, when they're displayed in a file browser, they're completely out of order. However, since they weren't named in a completely random manner, I can use regex to properly rename them, once I understand all the subpatterns that went into it. So far I only understood two.

So I'm going to create a program to verify the pattern of each filename, make a set out of it with a sample of each name, and then try to understand the patterns and subpatterns.

def get_pattern(name):
    pattern = []
    for letter in name:
        if letter.isalpha():
            pattern.append('a')
        elif letter.isdigit():
            pattern.append('0')
        else:
            pattern.append(letter)
    return ''.join(pattern)


It's pretty straightforward to loop through the images and then check the pattern for each image.

My questions:


Is there a way to make the code more concise? Maybe using a dictionary?  
Is there something in re or other easily available library that does this? Is using isalpha() and isdigit() the way to go here?
This will make your example work in the same way you intended to.

patterns = []
patterns.append(lambda x: 'A' if x.isalpha() else x)
patterns.append(lambda x: '0' if x.isdigit() else x)

def get_pattern(name):
    for pattern in patterns:
        name = map(pattern, name)
    return ''.join(name)


Additionally you can register new patterns. Here is an example:

&gt;&gt;&gt; get_pattern('Mario Cesar 2013')
'AAAAA AAAAA 0000'


About your objective, if you want just to sanitize the file names, will be the best to use a different approach, look at https://github.com/ksze/sanitize