import os
defsearch_file(dir,sname):if sname in os.path.split(dir)[1]: #检验文件名里是否包含sname
print(os.path.relpath(dir)) #打印相对路径,相对指相对于当前路径 if os.path.isfile(dir): # 如果传入的dir直接是一个文件目录 他就没有子目录,就不用再遍历它的子目录了returnfor dire in os.listdir(dir): # 遍历子目录 这里的dire为当前文件名
search_file(os.path.join(dir,dire),sname) #jion一下就变成了当前文件的绝对路径# 对每个子目录路劲执行同样的操作
其中:
os.path.isfile(path)
Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be trueforthe same path.