开发者

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

开发者 https://www.devze.com 2023-04-07 13:41 出处:网络
so i have a os.walk code search = self.search.get_text() top = \'/home/bludiescript/tv-shows\' for dirpath, dirnames, filena开发者_运维百科mes in os.walk(top):

so i have a os.walk code

search = self.search.get_text()
top = '/home/bludiescript/tv-shows'
for dirpath, dirnames, filena开发者_运维百科mes 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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号