编写一个程序,用户输入文件名以及开始搜索的路径,搜索该文件是否存在。如遇到文...
import os
def my_file(temp, file):
dir1 = os.listdir(temp)
for i in dir1:
if file == i:
print(os.path.dirname(i))
else:
if os.path.isdir(i):
my_file(i, file)
else:
print('没有找到该文件')
break
temp = input('请输入待查找的的初始目录:')
file = input('请输入要查找的目标文件:')
my_file(temp, file)
####哪里错了呜呜呜
import os
'''
返回值: 找到 路径, 没找到 ''
'''
def my_file(_dir, file):
for i in os.listdir(_dir):
path = os.path.join(_dir, i) # 重组路径
if os.path.isdir(path): # 文件夹
return my_file(path, file)
else: # 文件
if file == i:
return path
return ''
temp = input('请输入待查找的的初始目录:')
file = input('请输入要查找的目标文件:')
print(my_file(temp, file))
页:
[1]