|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是我的
import os
def search_file(path,file):
for each_path in os.walk(path):
for each_file in each_path[2]:
if each_file == file:
print(each_path[0]+os.sep+each_file)
search_path = input('请输入待查找的初始目录')
file_name = input(('请输入需要查找的文件名'))
search_file(search_path,file_name)
小甲鱼的
import os
def search_file(start_dir, target) :
os.chdir(start_dir)
for each_file in os.listdir(os.curdir) :
if each_file == target :
print(os.getcwd() + os.sep + each_file) # 使用os.sep是程序更标准
if os.path.isdir(each_file) :
search_file(each_file, target) # 递归调用
os.chdir(os.pardir) # 递归调用后切记返回上一层目录
start_dir = input('请输入待查找的初始目录:')
target = input('请输入需要查找的目标文件:')
search_file(start_dir, target)
我这里没有遇到拒绝访问的情况,估计是walk方法和list方法访问权限不一样吧
不过我发现你的代码有问题,只能查找一层目录,不支持递归查找下一层目录
|
-
这是我的代码运行结果
-
小甲鱼的运行结果,为什么小甲鱼的代码给出这样的结果
|