import os, pprint def get_dir_file(root_dir, get_key, key): for x in os.listdir(root_dir): # 获取目录下的所有文件和目录名称 if key in x: # 判断关键字是否在文件上 get_key.append(x) if os.path.isdir(x): # 判断是否为目录,并递归所有目录 get_dir_file(x, get_key, key) if __name__ == '__main__': root_dir = '.' # 赋值当前目录 get_key = [] key = '.py' # 关键字复制 get_dir_file(root_dir, get_key, key) # 函数调用 pprint.pprint(get_key)
Sign in to make a reply
尴尬村村长