fnmatch does not work with variables but with static strings
Question: The following code does not find any of the patterns defined in the file
`patterns`.

    
    
    #!/usr/bin/env python
    
    import os
    import fnmatch
    
    patternFile = open('patterns', 'r')
    patterns = patternFile.readlines()
    for filename in os.listdir('.'):
        for pattern in patterns:
            if fnmatch.fnmatch(filename, pattern):
                print "FOUND!"
    

My `pattern` file looks like

    
    
    *test
    foo
    

The following modified code with a static pattern string works. As expected, a
file named `foofile` could be found.

    
    
    patternFile = open('patterns', 'r')
    patterns = patternFile.readlines()
    for filename in os.listdir('.'):
        if fnmatch.fnmatch(filename, '*test'):
            print "FOUND!"
    

Does anybody know the problem?


Answer: `readlines` includes newline character `\n` at the end of each line, you need
to do:

    
    
    if fnmatch.fnmatch(filename, pattern.strip('\n\r'):
    

