1、编写一个程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
#!/usr/bin/python
# -*-* coding: utf-8 -*-
import os
def findfile(key_name,dir_path):
filename_list = [x for x in os.listdir(dir_path) if os.path.isfile(x)]
for filename in filename_list:
if filename.find(key_name) != -1:
print(os.path.join(dir_path,filename))
else:
pass
dirname_list = [ x for x in os.listdir(dir_path) if os.path.isdir(x)]
for dirname in dirname_list:
nextdir_path=os.path.join(dir_path,dirname)
findfile(key_name,nextdir_path)
def main():
dir_path = input('请输入绝对路径:')
key_name = input('请输入要查找的字符串:')
result = findfile(key_name, dir_path)
if __name__ == '__main__':
main()