输入路径,递归遍历路径下文件夹/文件
代码如下:
from pathlib import Path while True: folder = input("搜索文件的路径:") folder =
Path(folder.strip()) if folder.exists(): break else: print("输入的路径不准确,重新输入")
while True: search = input("请输入文件夹或文件的名字:") result =
list(folder.rglob(f'*{search}*')) if not result:
print("在{floder}下未找到关键字名称的{search}的文件夹或文件") continue else: result_folder=[]
result_file=[] for i in (result): if i.is_dir(): result_folder.append(i) else:
result_file.append(i) if result_folder: print("查找包含关键字{search}的文件夹有:") for i in
(result_folder): print(i) break if result_file: print("查找包含关键字{search}的文件有:")
for i in (result_file): print(i) break