零基础python老版第三十讲作业2
import osdef 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)
为什么小甲鱼这里使用了os.path模块,却不用导入模块import os.path呢,我在系统上试运行了也能正常运行 os模块里包含os.path
准确的说,os模块里,包含了所有名为“os.某某”的子模块
页:
[1]