|
8鱼币
- import os
- def test1(path,file_name):
- list1 = os.listdir(path)
- count = len(list1)
- for each in range(count):
- tuple1 = os.path.splitext(path + '\\' + list1[each])
- if tuple1[1] != '':
- if list1[each] == file_name:
- print(path + '\\' + list1[each])
- else:
- test1(path +'\\'+ list1[each],file_name)
- path = input('请给定待查找初始路径:')
- file_name = input('请输入文件名:')
- test1(path,file_name)
复制代码
本帖最后由 nahongyan1997 于 2021-6-25 17:34 编辑
我给你小改了一下: 主要是路径的拼接
- import os
- def test1(path,file_name):
- list1 = os.listdir(path)
- count = len(list1)
- for each in range(count):
- tuple1 = os.path.splitext(os.path.join(path,list1[each])) # 拼接路径记得用 os.path.join 函数,血粼粼的教训
- if tuple1[1] != '':
- if list1[each] == file_name:
- print(os.path.join(path,list1[each]))
- else:
- test1(os.path.join(path,list1[each]),file_name)
- path = input('请给定待查找初始路径:')
- file_name = input('请输入文件名:')
- test1(path,file_name)
复制代码
我发现你的代码压根就是错的,我给你都改好了:
- import os
- def test1(path,file_name):
- list1 = os.listdir(path)
- for each in list1:
- name,format_ = os.path.splitext(each) # 这里直接splitext就可以
- if format_ != '' and each == file_name:
- print(os.path.join(path,each))
-
- path = input('请给定待查找初始路径:')
- file_name = input('请输入文件名:')
- test1(path,file_name)
复制代码
|
最佳答案
查看完整内容
我给你小改了一下: 主要是路径的拼接
我发现你的代码压根就是错的,我给你都改好了:
|