something like this
Also note I don't know if there is better approach from business point of view to achieve your ultimate goal. My example is what you asked for - create multiple config files
names = ['ligand1.inp','ligand2.inp','ligand3.inp','ligand4.inp'] for i, name in enumerate(names, start=1): with open('CONFIG.inp') as cf, open(f'COFIG{i}.inp', 'w') as f: for j, line in enumerate(cf, start=1): if j == 3: line = line.replace('ligand.inp', name) f.write(line)if you show what ligand names look like it may be possible to avoid explicit list of names. Although your approach - reading the full file in memory then replace ligand.inp is perfectly OK if you are sure there is only one occurrence of ligand.inp in the file, I preferred to make sure the replacement is only in line 3.
Also note I don't know if there is better approach from business point of view to achieve your ultimate goal. My example is what you asked for - create multiple config files
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs