鱼C论坛

 找回密码
 立即注册
查看: 1912|回复: 1

[已解决]文件搜索查找问题求助

[复制链接]
发表于 2017-11-24 17:33:13 | 显示全部楼层 |阅读模式

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

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

x
要实现的功能是:搜索指定路径里的文件,看文件是否存在,如果有子文件夹在子文件夹里继续搜索。
下面的代码能符合要求。但如果把第9行注释掉输出的结果一样是为什么?

  1. def searchfile(f_path,f_name):  #搜索文件函数
  2.     os.chdir(f_path)

  3.     for i in os.listdir(os.curdir):
  4.         if i == f_name:
  5.             print(os.getcwd() + '\\' + f_name)
  6.         if os.path.isdir(i):
  7.             searchfile(i,f_name)
  8.             os.chdir(os.pardir)
  9.             


  10. file_path =  input('Please input the start search path: ')
  11. file_name =  input('Please input the file name: ')
  12. searchfile(file_path,file_name)
复制代码


输出结果:
Please input the start search path: D:\\test

Please input the file name: file.txt
D:\test\file.txt
D:\test\test1\test2\file.txt
D:\test\test1\test2\test3\file.txt

注释掉第9行之后
  1. def searchfile(f_path,f_name):  #搜索文件函数
  2.     os.chdir(f_path)
  3.    
  4.     for i in os.listdir(os.curdir):
  5.         if i == f_name:
  6.             print(os.getcwd() + '\\' + f_name)
  7.         if os.path.isdir(i):
  8.             searchfile(i,f_name)
  9.             os.chdir(os.pardir)
  10.             


  11. file_path =  input('Please input the start search path: ')
  12. file_name =  input('Please input the file name: ')
  13. searchfile(file_path,file_name)
复制代码

输出结果:
Please input the start search path: D:\\test

Please input the file name: file.txt
D:\test\file.txt
D:\test\test1\test2\file.txt
D:\test\test1\test2\test3\file.txt


二者没有什么差别,那这句话的作用是啥?
最佳答案
2017-11-24 18:05:39
因为恰好没有某一个文件夹中的不同文件夹有目标文件,所以输出才一样
它的作用是返回上一级目录
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-11-24 18:05:39 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
因为恰好没有某一个文件夹中的不同文件夹有目标文件,所以输出才一样
它的作用是返回上一级目录
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-4 07:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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