4

so i have a os.walk code

search = self.search.get_text()
top = '/home/bludiescript/tv-shows'
for dirpath, dirnames, filenames in os.walk(top):
  for filename in filenames:
    if fnmatch.fnmatch(filename, search)
      print os.path.join([dirpath, filename])

on python docs it shows you can match any seq of chars with [seq] pattern but no matter how i try to implement that it give so kind of error or no results at all.

so what would be the correct implementation to match the seq of cars in search so it will print out the file or files that match

implementations i tried

if fnmatch.fnmatch(filename, [search]) error i got was typeerror unhasable type : 'list'
if fnmatch.fnmatch[filename, search] error i got was typeerror fnmatch function is not subscriptable
if fnmatch.fnmatch([filename, search]) error typeerror fnmatch takes two arguments  1 given
if fnmatch.fnmatch([filename], search) error typeerror expected string or buffer
if fnmatch.fnmatch([search], filename) error typeerror expected string or buffer 
if fnmatch.fnmatch(filename, search, [seq]) error nameerror global name seq not defined

if fnmatch.fnmatch(filename, '[search]')

no errors but did not produce any results

search values

hello, mkv, merry 1, 2, 3, 4, 5, 6, 7, etc...

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user961559
  • 109
  • 1
  • 3
  • 9

1 Answers1

14

fnmatch implements the Unix shell wildcard syntax. So whatever you can type in an ls command (for example) will work:

>>> fnmatch.fnmatch("qxx", "[a-z]xx")
True
>>> fnmatch.fnmatch("abc", "a??")
True
>>> fnmatch.fnmatch("abcdef", "a*f")
True
>>> fnmatch.fnmatch("abcdef", "a*[f-k]")
True

Keep in mind, fnmatch is purely a string matching operation. If you find it more convenient to use a different pattern style, for example, regexes, then simply use regex operations to match your filenames.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • thank you so much for pointing me towards regular expression re.findall is what i was looking for . also i found out it that its case sensitive which i didn't realize. – user961559 Sep 24 '11 at 21:59