import os l=["AIMD_2.5","AIMD_2.6", "AIMD_2.7", "AIMD_2.8", "AIMD_2.9", "AIMD_3.0", "AIMD_3.1", "AIMD_3.2", "AIMD_3.3"] for d in l: os.system("cp -f ./lagrange.py " + d) os.system("cd " + d + "; python lagrange.py > ../tmp.lag") f=open("tmp.lag", "r") for line in f: if "Shake mean force (SD)" in line: print d, line f.close()