鱼C论坛

 找回密码
 立即注册
查看: 2533|回复: 2

[已解决]python 30 课后作业

[复制链接]
发表于 2021-11-15 14:22:26 | 显示全部楼层 |阅读模式

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

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

x
  1. import os
  2. import os.path
  3. def find_file(file_path,file_name):
  4.      os.chdir(file_path)
  5.      os.listdir(file_path)

  6.      for each_one in os.listdir(file_path): #对于输入路径中的二级目录中的所有文件   
  7.           if file_name == each_one:         #如果路径文件下的二级目录中存在需要找的文件
  8.                print(file_path + file_name)
  9.           else:
  10.                find_file(each_one,file_name)#如果二级目录中没有,需要在二级目录下的三级目录中找
  11.                os.chdir(os.pardir)
  12. file_path = input('请输入待查找的初始目录:')
  13. file_name = input('请输入待查找问价的名字:')
  14. find_file(file_path,file_name)
  15.                
复制代码









大佬们,代码该怎么改
最佳答案
2021-11-15 15:06:54
本帖最后由 myqf123 于 2021-11-15 15:48 编辑

你这程序没有进行文件和目录文件判断,对于既不是你要找的文件,也不是单纯的目录文件,你的程序是无法识别的,比如你的目录下第一个文件是:1.py,它既不是你要找的文件,但又不是目录文件,而你的程序会把这个文件当做路径传递给自身,自然会引起错误。
微信截图_20211115142134.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-15 15:06:54 | 显示全部楼层    本楼为最佳答案   
本帖最后由 myqf123 于 2021-11-15 15:48 编辑

你这程序没有进行文件和目录文件判断,对于既不是你要找的文件,也不是单纯的目录文件,你的程序是无法识别的,比如你的目录下第一个文件是:1.py,它既不是你要找的文件,但又不是目录文件,而你的程序会把这个文件当做路径传递给自身,自然会引起错误。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-11-15 15:09:49 | 显示全部楼层
本帖最后由 jackz007 于 2021-11-15 15:30 编辑
  1. #coding:gbk

  2. import os

  3. def find_file(file_path , file_name):
  4.     c = 0
  5.     try:
  6.         for each_one in os . listdir(file_path):
  7.             x = os . path . join(file_path , each_one)
  8.             if os . path . isfile(x):
  9.                 if each_one . lower() == file_name . lower():
  10.                     print(x)
  11.                     c += 1
  12.             elif os . path . isdir(x):
  13.                 c += find_file(x , file_name)
  14.     except Exception as e:
  15.         pass
  16.     return c

  17. file_path = input('请输入待查找的初始目录 : ')
  18. file_name = input('请输入待查找文件的名字 : ')
  19. print('一共找到了 %d 个目标文件.' % find_file(file_path , file_name))
复制代码

        运行实况:
  1. D:\0002.Exercise\Python>python x.py
  2. 请输入待查找的初始目录 : C:\
  3. 请输入待查找文件的名字 : notepad.exe
  4. C:\Windows\notepad.exe
  5. C:\Windows\System32\notepad.exe
  6. C:\Windows\SysWOW64\notepad.exe
  7. C:\Windows\winsxs\amd64_microsoft-windows-notepadwin_31bf3856ad364e35_6.1.7600.16385_none_9ebebe8614be1470\notepad.exe
  8. C:\Windows\winsxs\amd64_microsoft-windows-notepadwin_31bf3856ad364e35_6.1.7601.18917_none_a0f2c3fc11a9f24c\notepad.exe
  9. C:\Windows\winsxs\amd64_microsoft-windows-notepadwin_31bf3856ad364e35_6.1.7601.23120_none_a16a66f72ad62fe8\notepad.exe
  10. C:\Windows\winsxs\amd64_microsoft-windows-notepad_31bf3856ad364e35_6.1.7600.16385_none_cb0f7f2289b0c21a\notepad.exe
  11. C:\Windows\winsxs\amd64_microsoft-windows-notepad_31bf3856ad364e35_6.1.7601.18917_none_cd438498869c9ff6\notepad.exe
  12. C:\Windows\winsxs\amd64_microsoft-windows-notepad_31bf3856ad364e35_6.1.7601.23120_none_cdbb27939fc8dd92\notepad.exe
  13. C:\Windows\winsxs\wow64_microsoft-windows-notepad_31bf3856ad364e35_6.1.7600.16385_none_d5642974be118415\notepad.exe
  14. C:\Windows\winsxs\wow64_microsoft-windows-notepad_31bf3856ad364e35_6.1.7601.18917_none_d7982eeabafd61f1\notepad.exe
  15. C:\Windows\winsxs\wow64_microsoft-windows-notepad_31bf3856ad364e35_6.1.7601.23120_none_d80fd1e5d4299f8d\notepad.exe
  16. 一共找到了 12 个目标文件.

  17. D:\0002.Exercise\Python>
复制代码

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 22:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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