user2125827 user2125827 - 1 year ago 65
Python Question

pattern match failing while finding a file

I am trying to find a file

in a given path as below but somehow the
fnmatch.fnmatch(name, pattern)
is failing,can anyone suggest how to fix this?

import os, fnmatch

path = "\\\\location1\\build1\\obj\\vendor\\qcom\\opensource\\tech"

def find(pattern, path):
result = []
for root, dirs, files in os.walk(path):
for name in files:
#print name
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result

result = find('*', path)
print result//prints empty string

cco cco
Answer Source

Your match pattern is *, but the name you're looking for is

fnmatch patterns aren't the same as a regex, so . only matches a literal ., not 'any single character' as it would in a regex.

using * or * as the pattern should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download