鱼C论坛

 找回密码
 立即注册
查看: 2988|回复: 3

030课后作业

[复制链接]
发表于 2019-11-27 21:13:43 | 显示全部楼层 |阅读模式

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

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

x
  1. import os
  2. def search_file(primary_list, target_file):
  3.     os.chdir(primary_list)
  4.     all_files = os.listdir(os.curdir())
  5.     for each_file in all_files:
  6.         if each_file== target_file:
  7.             print(os.getcwd(each_file) + os.sep + each_file)
  8.         if os.path.isfile(each_file):
  9.             search_file(each_file, target_file)
  10.             os.chdir(os.pardir())


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


报错说:TypeError: 'str' object is not callable
为什么不能调用??
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-11-27 21:19:24 | 显示全部楼层
os.curdir是一个字符串,不能带括号。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2019-11-27 22:15:28 | 显示全部楼层
os.chdir(os.pardir())
os.chdir(os.curdir())

多了对括号,要去掉。

  1. import os
  2. def search_file(primary_list, target_file):
  3.     os.chdir(primary_list)
  4.     all_files = os.listdir(os.curdir)
  5.     for each_file in all_files:
  6.         if each_file== target_file:
  7.             print(os.getcwd(each_file) + os.sep + each_file)
  8.         if os.path.isfile(each_file):
  9.             search_file(each_file, target_file)
  10.             os.chdir(os.pardir)


  11. primary_list = input("请输入待查找的初始目录:")
  12. target_file = input('请输入需要查找的目标文件:')
  13. search_file(primary_list, target_file)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-11-28 19:40:08 | 显示全部楼层
这个问题改正了,但是还是运行不了
NotADirectoryError: [WinError 267] 目录名称无效。: 'bfsvc.exe'
显示line3.9.15有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-26 02:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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