鱼C论坛

 找回密码
 立即注册
查看: 2056|回复: 10

[已解决]请教一下,这个代码错哪了了。。脑子都绕晕了

[复制链接]
发表于 2021-6-25 17:10:28 | 显示全部楼层 |阅读模式
8鱼币
  1. import os
  2. def test1(path,file_name):
  3.     list1 = os.listdir(path)
  4.     count = len(list1)
  5.     for each in range(count):
  6.         tuple1 = os.path.splitext(path + '\\' + list1[each])
  7.         if tuple1[1] != '':
  8.             if list1[each] == file_name:
  9.                 print(path + '\\' + list1[each])
  10.         else:
  11.             test1(path +'\\'+ list1[each],file_name)
  12. path = input('请给定待查找初始路径:')
  13. file_name = input('请输入文件名:')
  14. test1(path,file_name)
复制代码
最佳答案
2021-6-25 17:10:29
本帖最后由 nahongyan1997 于 2021-6-25 17:34 编辑

我给你小改了一下:  主要是路径的拼接
  1. import os
  2. def test1(path,file_name):
  3.     list1 = os.listdir(path)
  4.     count = len(list1)
  5.     for each in range(count):
  6.         tuple1 = os.path.splitext(os.path.join(path,list1[each]))   # 拼接路径记得用 os.path.join 函数,血粼粼的教训
  7.         if tuple1[1] != '':
  8.             if list1[each] == file_name:
  9.                 print(os.path.join(path,list1[each]))
  10.         else:
  11.             test1(os.path.join(path,list1[each]),file_name)
  12. path = input('请给定待查找初始路径:')
  13. file_name = input('请输入文件名:')
  14. test1(path,file_name)
复制代码


我发现你的代码压根就是错的,我给你都改好了:
  1. import os
  2. def test1(path,file_name):
  3.     list1 = os.listdir(path)
  4.     for each in list1:
  5.         name,format_ = os.path.splitext(each)    # 这里直接splitext就可以
  6.         if format_ != '' and each == file_name:
  7.             print(os.path.join(path,each))
  8.             
  9. path = input('请给定待查找初始路径:')
  10. file_name = input('请输入文件名:')
  11. test1(path,file_name)
复制代码

最佳答案

查看完整内容

我给你小改了一下: 主要是路径的拼接 我发现你的代码压根就是错的,我给你都改好了:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-25 17:10:29 | 显示全部楼层    本楼为最佳答案   
本帖最后由 nahongyan1997 于 2021-6-25 17:34 编辑

我给你小改了一下:  主要是路径的拼接
  1. import os
  2. def test1(path,file_name):
  3.     list1 = os.listdir(path)
  4.     count = len(list1)
  5.     for each in range(count):
  6.         tuple1 = os.path.splitext(os.path.join(path,list1[each]))   # 拼接路径记得用 os.path.join 函数,血粼粼的教训
  7.         if tuple1[1] != '':
  8.             if list1[each] == file_name:
  9.                 print(os.path.join(path,list1[each]))
  10.         else:
  11.             test1(os.path.join(path,list1[each]),file_name)
  12. path = input('请给定待查找初始路径:')
  13. file_name = input('请输入文件名:')
  14. test1(path,file_name)
复制代码


我发现你的代码压根就是错的,我给你都改好了:
  1. import os
  2. def test1(path,file_name):
  3.     list1 = os.listdir(path)
  4.     for each in list1:
  5.         name,format_ = os.path.splitext(each)    # 这里直接splitext就可以
  6.         if format_ != '' and each == file_name:
  7.             print(os.path.join(path,each))
  8.             
  9. path = input('请给定待查找初始路径:')
  10. file_name = input('请输入文件名:')
  11. test1(path,file_name)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-25 17:11:24 | 显示全部楼层
题目是:编写一个程序,用户输入文件名以及开始搜索的路径,搜索该文件是否存在。如遇到文件夹,则进入文件夹继续搜索

报错是:list1 = os.listdir(path)
NotADirectoryError: [WinError 267] 目录名称无效。: 'D:\\\\python3\\Lib\\ctypes\\macholib\\fetch_macholib'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-25 17:23:18 | 显示全部楼层
因为你的路径D:\\\\有问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-25 20:20:36 | 显示全部楼层
fish_nian 发表于 2021-6-25 17:23
因为你的路径D:\\\\有问题

我是输入的D:\\,不知道为啥报错报的D:\\\\..
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-25 20:21:18 | 显示全部楼层
nahongyan1997 发表于 2021-6-25 17:29
我给你小改了一下:  主要是路径的拼接

谢谢谢谢大佬
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-25 20:25:32 | 显示全部楼层
nahongyan1997 发表于 2021-6-25 17:10
我给你小改了一下:  主要是路径的拼接

改了还是报错。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-25 20:26:13 | 显示全部楼层
nahongyan1997 发表于 2021-6-25 17:10
我给你小改了一下:  主要是路径的拼接
  1. import os


  2. def test1(path, file_name):
  3.     list1 = os.listdir(path)
  4.     for each in list1:
  5.         name, format_ = os.path.splitext(each)  # 这里直接splitext就可以
  6.         if format_ != '' and each == file_name:
  7.             print(os.path.join(path, each))
  8.         else:
  9.             test1(os.path.join(path,each),file_name)

  10. path = input('请给定待查找初始路径:')
  11. file_name = input('请输入文件名:')
  12. test1(path, file_name)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-25 20:27:01 | 显示全部楼层
nahongyan1997 发表于 2021-6-25 17:10
我给你小改了一下:  主要是路径的拼接

NotADirectoryError: [WinError 267] 目录名称无效。: 'D:\\\\python3\\DLLs\\libcrypto-1_1.dll'
报错又变成这个了,这种报错是啥意思。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-6-25 21:18:47 | 显示全部楼层
那你的问题到底解决了没有啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-6-25 21:24:48 | 显示全部楼层
nahongyan1997 发表于 2021-6-25 21:18
那你的问题到底解决了没有啊

改过来了,解决了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-1 03:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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