import os root = "images\\pressure" for path, subdirs, files in os.walk(root): for name in files: if "1000rpm" not in name: continue filePath = os.path.join(path, name) str = "\\begin{subfigure}{0.45\\linewidth}\n \\centering\n \\includegraphics[width=0.95\\linewidth]{" + filePath.replace("\\", '/') + "}\n \\caption{" + name.replace("Percent", "\\%").replace(".png", "").replace("m_s", "m/s") + "}\n\\end{subfigure}" print(str)