<pre>
import os
def my_find(*s):#可变参数满足递归
#os.chdir(path)更改当前路径为path
path=s[0]
global relpath
if len(s)>1:
relpath=s[1]
global key
key=input('请输入要查询的文件名:\n')
for x in os.listdir(path):
if os.path.isfile(path+'\'+x) and (key in x):#os.path.splitext(x)[1]==key:
print(x)
print('relpath:%s'%os.path.relpath(path,relpath))#os.path.relpath(path[,start])获得path相对于start的相对路径
if os.path.isdir(path+'\'+x):
my_find(path+'\'+x)
path=input('请输入要查询的目录路径:\n')
my_find(path,path)
</pre>
Exmlyshy
<pre> import os def my_find(*s):#可变参数满足递归 #os.chdir(path)更改当前路径为path path=s[0] global relpath if len(s)>1: relpath=s[1] global key key=input('请输入要查询的文件名:\n') for x in os.listdir(path): if os.path.isfile(path+'\'+x) and (key in x):#os.path.splitext(x)[1]==key: print(x) print('relpath:%s'%os.path.relpath(path,relpath))#os.path.relpath(path[,start])获得path相对于start的相对路径 if os.path.isdir(path+'\'+x): my_find(path+'\'+x) path=input('请输入要查询的目录路径:\n') my_find(path,path) </pre>