Search code examples
pythonsearchpygtkglobos.walk

how to implement glob.glob


currently my os.walk code list's all the files in all directories under the specified directory.

top = /home/bludiescript/tv-shows
        for dirpath, dirnames, filenames in os.walk(top):
               for filename in filenames:
                  print os.path.join([dirname, filename])

so how could i add

glob.glob(search)
search = self.search.get_text

to search for the pattern that i type in the gtk.Entry

or is this something that would not work with my current code


Solution

  • You don't want glob.glob for this; it checks against names in the directory, which you've already retrieved. Instead, use fnmatch.fnmatch to match your pattern against the list of pathnames you got from os.walk (probably before you add the path).

    for filename in filenames:
        if fnmatch.fnmatch(filename, search):
            print os.path.join([dirname, filename])