编写一个程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出绝对路径

import os  # 引入os

def search_file(path, str): # 传入当前的绝对路径以及指定字符串
# 首先先找到当前目录下的所有文件
for file in os.listdir(path): # os.listdir(path) 是当前这个path路径下的所有文件的列表
this_path = os.path.join(path, file)
if os.path.isfile(this_path): # 判断这个路径对应的是目录还是文件,是文件就走下去
if str in file:
return this_path
else: # 不是就再次执行这个函数,递归下去
search_file(this_path, str) # 递归下去
else:
return None


print(search_file(os.path.abspath("."), "c"))

猜你喜欢

转载自www.cnblogs.com/luchenhui/p/10880516.html