查找某个文件夹下指定后缀名的所有文件
import glob
glob.glob(os.path.join(target_dir, target_suffix))
glob.glob(os.path.join("./", "*.txt"))
查找某个文件夹及其子文件夹下指定后缀名的所有文件
import os
def findAllFilesWithSpecifiedSuffix(target_dir, target_suffix="txt"):
find_res = []
target_suffix_dot = "." + target_suffix
walk_generator = os.walk(target_dir)
for root_path, dirs, files in walk_generator:
if len(files) < 1:
continue
for file in files:
file_name, suffix_name = os.path.splitext(file)
if suffix_name == target_suffix_dot:
find_res.append(os.path.join(root_path, file))
return find_res
findAllFilesWithSpecifiedSuffix("./", "txt")