Aug-30-2018, 08:55 AM
(This post was last modified: Aug-30-2018, 09:24 AM by clarablanes.)
(Aug-29-2018, 08:17 PM)buran Wrote: something like this
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
Dear buran, I'm trying this code but it keeps giving me the following error:
File "CONFIG_iterator.py", line 3
with open('CONFIG.inp') as cf, open(f'CONFIG{i}.inp', 'w') as f:
SyntaxError: invalid syntax
(Aug-29-2018, 08:30 PM)Gribouillis Wrote: You could simply use Python's format method
template = """\ TEMPOP /home/c.blanes/FlexAID2/FlexAID_Output PDBNAM /home/c.blanes/FlexAID2/Processed_files/receptor.inp.pdb INPLIG /home/c.blanes/FlexAID2/Processed_files/{ligand}.inp RNGOPT LOCCLF /home/c.blanes/FlexAID2/Binding_sites/binding_site.pdb OPTIMZ 9999 - -1 OPTIMZ 9999 - 0 OPTIMZ 9999 - 1 OPTIMZ 9999 - 2 OPTIMZ 9999 - 3 OPTIMZ 9999 - 4 ... """ names = ['foo', 'bar', 'baz'] for idx, name in enumerate(names, 1): with open('CONFIG{}.inp'.format(idx), 'w') as outfile: outfile.write(template.format(ligand=name))
Dear Gribouillis,
I am just beginning with Python to be applied to science so I am very lost. Should I add your code to the CONFIG file?. If I do this, how can execute it?. I mean, if I write your code in a different document, I save it as a .py and I execute de CONFIG file to be able to obtain the other CONFIGS. But, if I add the code to the bottom of the CONFIG file, I don't know how to execute it.
Thank you for your patient...