Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fnmatch how exactly do you implement the match any chars in seq pattern

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...

like image 370
user961559 Avatar asked Dec 28 '22 13:12

user961559


1 Answers

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.

like image 103
Ned Batchelder Avatar answered Mar 23 '23 01:03

Ned Batchelder