Jul-19-2017, 04:09 PM
I don't think you need os.walk(), since you're not recursing into those directories (it would work, it's just more complicated than you need). glob.glob() could do this just fine.
import glob, pathlib destination = "/this/other/different/path/to/{0}/filetomodify" for ref in glob.glob("/this/path/to/*/refFile"): path = pathlib.Path(ref) folder = path.parent.name with open(ref) as refFile: with open(destination.format(folder), "w") as file_to_modify: for line in refFile: # line formatting here print(line, file=file_to_modify)