import os import glob def search_file(file_path, file): # file_path指定路径, file指定文件名 path = file_path file_list = [] for root, dirs, files in os.walk(path): file_pattern = os.path.join(root, file) for f in glob.glob(file_pattern): file_list.append(f) return file_list def into(f): pass # with open(f, 'wb') as i: # pass # f = str.encode(f) # i.write(f) def func(file_list): if not file_list: print("没有这个文件") else: print("一共找出 %d 个文件" % len(file_list)) print("找出的文件如下: ") for f in file_list: print(f) into(f) # with open(f, 'wb') as i: # f = str.encode(f) # i.write(f) print('查找文件结束') if __name__ == '__main__': file_path = r'C:\Users\admin\Desktop\newscore' file = 'id_json.txt' file_list = search_file(file_path, file) func(file_list)