rootpath = "d:/temp/dirtest" # 设置需要遍历的文件目录 from database_tools import * texfiles, texpath = GetValidTexFiles(rootpath) for path in texpath: output_dict = {} for texfile in [f for f in texfiles if f.startswith(path)]: output_dict[os.path.split(texfile)[1]] = re.findall(r"\((\d{6})\)",ReadTextFile(texfile)) output = "" for name in sorted(output_dict): output += name + "\n" output += "\n".join(output_dict[name]) output += "\n\n\n" SaveTextFile(output,os.path.join(path,"题号清点.txt")) # 在每个有含题号的.tex文件所在的目录中生成 题号清点.txt print("已完成文件夹 %s 中的题号清点"%path)