鱼C论坛

 找回密码
 立即注册
查看: 2297|回复: 5

[已解决]030文件系统 第2题 递归调用后为什么返回上一层目录?

[复制链接]
发表于 2019-12-30 12:58:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如答案中代码所示,不太能偶理解此处的递归调用。我尝试用非递归的方法做,但是没做出来。先将代码贴在下面,求指教。

小甲鱼的代码:
  1. import os

  2. def search_file(start_dir, target) :
  3.     os.chdir(start_dir)
  4.    
  5.     for each_file in os.listdir(os.curdir) :
  6.         if each_file == target :
  7.             print(os.getcwd() + os.sep + each_file) # 使用os.sep是程序更标准
  8.         if os.path.isdir(each_file) :
  9.             search_file(each_file, target) # 递归调用
  10.             os.chdir(os.pardir) # 递归调用后切记返回上一层目录

  11. start_dir = input('请输入待查找的初始目录:')
  12. target = input('请输入需要查找的目标文件:')
  13. search_file(start_dir, target)
复制代码


我的代码:
  1. path_origin = input('请输入待查找的初始目录:')
  2. file_object = input('请输入需要查找的目标文件')
  3. while os.path.exists(path_origin) != 1:
  4.     path_origin = input('路径不存在,请输入正确路径')
  5. while os.path.exists(file_object) !=1:
  6.     file_object = input('文件不存在,请输入正确文件名')

  7. import os
  8. list_file = os.listdir(path_origin)
  9. while file_object not in path_origin:
  10.     for each_file in list_file:
  11.         if os.path.isfile(each_file):
  12.             add = (os.path.split(each_file))[1]
  13.             path_origin = os.path.join(path_origin,add)
  14.             list_file =os.listdir(path_origin)
  15.         elif os.path.ispath(each_file):
  16.             print('原路径下不存在该目标文件')
  17.             break
  18.    


  19. print(path_origin)


  20.         
复制代码
最佳答案
2019-12-30 14:48:58
本帖最后由 jackz007 于 2019-12-30 15:20 编辑
  1. def search_file(start_dir, target) :
  2.     os . chdir(start_dir)                     # 这条指令无条件改变了当前路径
  3. . . . . . .   
  4.             search_file(each_file, target)    # 递归会把当前路径变更到 each_file,由于 each_file 是当前目录的次级子目录
  5.             os.chdir(os . pardir)             # 所以,返回父目录就是恢复当前路径  
复制代码

        如果把这个代码改成下面这样,递归调用之后,就不再需要返回上级目录了:
  1. import os

  2. def search_file(start_dir, target) :
  3.     cwd = os . getcwd()    # 改变当前路径之前,先记录原始当前路径     
  4.     os . chdir(start_dir)  # 改变当前路径
  5.    
  6.     for each_file in os.listdir(os.curdir) :
  7.         if each_file == target :
  8.             print(os . path . join(os . getcwd() , each_file)
  9.         if os.path.isdir(each_file) :
  10.             search_file(each_file , target)
  11.     os . chdir(cwd)        # 函数退出前,必须恢复原始当前路径

  12. start_dir = input('请输入待查找的初始目录:')
  13. target = input('请输入需要查找的目标文件:')
  14. search_file(start_dir, target)
复制代码


       如果只搜一层目录,那完全可以不用递归,如果要实现遍历所有子目录的效果,这递归恐怕就无法绕开了,当然,你也可以改用 os . walk() 遍历所有子目录,这个方法任何时候都不用递归。
  1. def search_file(start_dir , target) :
  2.     for root , dirs , files in os . walk(start_dir):
  3.         for file in files:
  4.             if file . lower() == target . lower():      # windows 文件名比较,忽略字母大小写。
  5.                 print(os . path . join(root , file))

  6. start_dir = input('请输入待查找的初始目录:')
  7. target = input('请输入需要查找的目标文件:')
  8. search_file(start_dir, target)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-30 14:48:58 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2019-12-30 15:20 编辑
  1. def search_file(start_dir, target) :
  2.     os . chdir(start_dir)                     # 这条指令无条件改变了当前路径
  3. . . . . . .   
  4.             search_file(each_file, target)    # 递归会把当前路径变更到 each_file,由于 each_file 是当前目录的次级子目录
  5.             os.chdir(os . pardir)             # 所以,返回父目录就是恢复当前路径  
复制代码

        如果把这个代码改成下面这样,递归调用之后,就不再需要返回上级目录了:
  1. import os

  2. def search_file(start_dir, target) :
  3.     cwd = os . getcwd()    # 改变当前路径之前,先记录原始当前路径     
  4.     os . chdir(start_dir)  # 改变当前路径
  5.    
  6.     for each_file in os.listdir(os.curdir) :
  7.         if each_file == target :
  8.             print(os . path . join(os . getcwd() , each_file)
  9.         if os.path.isdir(each_file) :
  10.             search_file(each_file , target)
  11.     os . chdir(cwd)        # 函数退出前,必须恢复原始当前路径

  12. start_dir = input('请输入待查找的初始目录:')
  13. target = input('请输入需要查找的目标文件:')
  14. search_file(start_dir, target)
复制代码


       如果只搜一层目录,那完全可以不用递归,如果要实现遍历所有子目录的效果,这递归恐怕就无法绕开了,当然,你也可以改用 os . walk() 遍历所有子目录,这个方法任何时候都不用递归。
  1. def search_file(start_dir , target) :
  2.     for root , dirs , files in os . walk(start_dir):
  3.         for file in files:
  4.             if file . lower() == target . lower():      # windows 文件名比较,忽略字母大小写。
  5.                 print(os . path . join(root , file))

  6. start_dir = input('请输入待查找的初始目录:')
  7. target = input('请输入需要查找的目标文件:')
  8. search_file(start_dir, target)
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +1 收起 理由
zltzlt + 5 + 5 + 1

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-12-30 19:46:57 | 显示全部楼层
如果不返回上一层目录,程序就会陷在子目录里出不来,会导致死循环。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

 楼主| 发表于 2019-12-31 00:44:18 | 显示全部楼层
jackz007 发表于 2019-12-30 14:48
如果把这个代码改成下面这样,递归调用之后,就不再需要返回上级目录了:


非常感谢大神的详细解释。
我对原来代码06到11行的的理解是:若是在第一层找到了target,就相当于在最大的钱袋子中找到了需要的金币。但是最大的袋子里面还有别的小袋子。如果是dir相当于是挖到了一个小钱袋,打开小钱袋,在其中找金币,若是找到了,还要回到最大的钱袋子找找看还有没有别的小袋子。同时对别的小袋子进行相同的操作。
感谢大神,感谢鱼C,在学编程的路上有你们真真是极好的!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-31 00:46:52 | 显示全部楼层
zltzlt 发表于 2019-12-30 19:46
如果不返回上一层目录,程序就会陷在子目录里出不来,会导致死循环。

谢谢你,请看我的“钱袋子”解释,我应该是理解了这句话了,嘻嘻
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-31 01:11:57 | 显示全部楼层
jackz007 发表于 2019-12-30 14:48
如果把这个代码改成下面这样,递归调用之后,就不再需要返回上级目录了:

os.walk()方法也太精辟了!先积累着了,谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 06:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表