以下是一个使用Python语言遍历文件夹和子文件夹进行通配符搜索的示例代码:
import os
import glob
def search_files(root_dir, pattern):
for root, dirs, files in os.walk(root_dir):
for file in files:
if glob.fnmatch.fnmatch(file, pattern):
print(os.path.join(root, file))
# 示例调用
search_files('/path/to/folder', '*.txt')
这段代码使用os.walk()
函数遍历指定的根目录及其子文件夹,然后使用glob.fnmatch.fnmatch()
函数进行通配符搜索。在搜索过程中,如果文件名与指定的通配符模式匹配,则将其完整路径打印出来。
你可以根据自己的需求修改root_dir
和pattern
参数来指定搜索的根目录和通配符模式。