可以使用 Python 的 os 模块和 glob 模块来实现搜索本地文件功能。具体代码如下:

import os
import glob

search_dir = '/path/to/search/dir'
search_pattern = '*.txt'

# 使用 glob 模块搜索指定目录下符合条件的文件
file_list = glob.glob(os.path.join(search_dir, search_pattern))

# 打印搜索结果
print('搜索到的文件:')
for file in file_list:
    print(file)

其中,search_dir 为待搜索的目录,search_pattern 为搜索的文件名匹配模式,如 *.txt 表示搜索所有扩展名为 txt 的文件。使用 os.path.join 函数拼接目录和文件名,得到完整的文件路径。然后使用 glob.glob 函数搜索符合条件的文件,返回一个文件路径列表。最后遍历文件列表,打印搜索结果。

一行 Python 代码实现本地文件搜索

原文地址: https://www.cveoy.top/t/topic/oiGy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录