如何实现 glob.glob
目前我的os.walk代码列出了指定目录下所有目录中的所有文件。
top = /home/bludiescript/tv-shows
for dirpath, dirnames, filenames in os.walk(top):
for filename in filenames:
print os.path.join([dirname, filename])
那么我如何添加
glob.glob(search)
search = self.search.get_text
搜索我在 gtk.Entry 中输入的模式
,或者这是否不适用于我当前的代码
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要
glob.glob
来实现此目的;它会检查您已经检索到的目录中的名称。相反,使用fnmatch.fnmatch
将您的模式与从os.walk
获取的路径名列表进行匹配(可能在添加路径之前)。You don't want
glob.glob
for this; it checks against names in the directory, which you've already retrieved. Instead, usefnmatch.fnmatch
to match your pattern against the list of pathnames you got fromos.walk
(probably before you add the path).您不需要
glob
,您需要fnmatch
。glob
完成了os.walk
已经完成的部分工作:检查磁盘以查找文件。fnmatch
是一个纯字符串操作:这个文件名与这个模式匹配吗?You don't want
glob
, you wantfnmatch
.glob
does part of the work thatos.walk
has already done: examine the disk to find files.fnmatch
is a pure string operation: does this filename match this pattern?