|
发表于 2020-2-25 11:45:14
|
显示全部楼层
本帖最后由 jackz007 于 2020-2-25 11:46 编辑
- import os
- def search_file(start_dir, target) :
- cwd = os . getcwd() # 添加此句,先保存当前路径
- os . chdir(start_dir) # 改变当前路径
-
- for each_file in os.listdir(os.curdir) :
- if each_file == target :
- print(os.getcwd() + os.sep + each_file)
- if os.path.isdir(each_file) :
- search_file(each_file, target)
- # os.chdir(os.pardir) # 函数 search_file() 不再改变当前路径,所以,必须删除这一句
- os . chdir(cwd) # 添加此句,退出函数前,再恢复当前路径
- start_dir = input('请输入待查找的初始目录:')
- target = input('请输入需要查找的目标文件:')
- search_file(start_dir, target)
复制代码 |
|