鱼C论坛

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

[已解决]目录查找

[复制链接]
发表于 2018-4-20 19:11:22 | 显示全部楼层 |阅读模式

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

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

x
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)
以上是30课后作业答案,不明白的是os.path.isdir()括号里不应该填路径名吗,为啥答案里写的是each_file文件名?
最佳答案
2018-4-20 19:14:45
each_file在listdir得来,
可能是文件名也可能是文件夹名
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-4-20 19:14:45 | 显示全部楼层    本楼为最佳答案   
each_file在listdir得来,
可能是文件名也可能是文件夹名
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-10 18:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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