currently my os.w开发者_Go百科alk 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
You don't want glob
, you want fnmatch
.
for dirpath, dirnames, filenames in os.walk(top):
for filename in filenames:
if fnmatch.fnmatch(filename, my_pattern):
print os.path.join([dirname, filename])
glob
does part of the work that os.walk
has already done: examine the disk to find files. fnmatch
is a pure string operation: does this filename match this pattern?
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])
精彩评论