284665 发表于 2020-4-25 11:01:18

求助:课后作业30讲第2题

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)

上面是小甲鱼的参考答案,可是我运行了却显示
Traceback (most recent call last):
File "<pyshell#10>", line 3, in <module>
    search_file(each_file, target) # 递归调用
File "<pyshell#2>", line 8, in search_file
    search_file(each_file, target) # 递归调用
File "<pyshell#2>", line 8, in search_file
    search_file(each_file, target) # 递归调用
File "<pyshell#2>", line 8, in search_file
    search_file(each_file, target) # 递归调用
File "<pyshell#2>", line 4, in search_file
    for each_file in os.listdir(os.curdir) :
PermissionError: 拒绝访问。: '.'

请问是哪儿出问题了呀

老八秘制 发表于 2020-4-25 11:08:09

你是不是访问C盘这类东西了,py没有权限的,换个盘试试

284665 发表于 2020-4-25 11:18:28

老八秘制 发表于 2020-4-25 11:08
你是不是访问C盘这类东西了,py没有权限的,换个盘试试

哇哦!谢谢你,真的是C盘没办法访问,那请问你知道如果必须要查找C盘的东西要怎么办呢

老八秘制 发表于 2020-4-25 13:54:15

284665 发表于 2020-4-25 11:18
哇哦!谢谢你,真的是C盘没办法访问,那请问你知道如果必须要查找C盘的东西要怎么办呢

https://zhidao.baidu.com/question/441786454.html

284665 发表于 2020-4-25 14:55:37

老八秘制 发表于 2020-4-25 13:54
https://zhidao.baidu.com/question/441786454.html

谢谢你
页: [1]
查看完整版本: 求助:课后作业30讲第2题