|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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)
红字处解释不太明白用意 我猜测是如果不返回上一层目录 或许for 循环那里 蓝字会改变 循环就乱了 为此我试验了一下 写了几行代码 但是无法运行 还请指教:
a=[0,1,2,3]
for i in a:
a.append(4)
if (len(a) > 15):
break
print(a)
报错: inconsistent use if tabs and spaces in indentation
|
|