I have a file named myfile.txt given below:
&cntrl
pTopt = 298.15, pdens = 0.997, prcut = 12.0, pion = t,
pihot = t, prQM = 5.5, prSM = 5.3, prQI=3.0, piguess = f,
pinit = t, pnstep = 5000, pnscale = 100,
pnstat = 5, pnout = 5, pnrst = 5, pioutc = t, pioutv = t, pnoutc=5,
pnoutv = 5,
msolute = t, nosa = 1, pichrg = t
gfileOut = 'as-1.out',
gfileEnout = 'as-1.en',
gfileInfo = 'as-1.info',
gfileStart = 'init.in',
gfileRst = 'as-1.rst',
gfileTraj = 'as-1.traj',
gfileVeloc = 'as-1.vel',
gfileQmen = 'as-1.qmen'
&end
Using the above given single file I want to create 10 files but I want to manipulate the values of last eight variables in a way that value of the variable in every new file changes as the number of file changes i.e if ten files are created then the value of last eight variables like gfileOut in tength file should be 'as-10.out'. For doing this I have a code given below:
#!/usr/bin/python3
for i in range(10):
f = open('file' +str(i)+'.txt','w')
f.write("&cntrl pTopt = 298.15, pdens = 0.997, prcut = 12.0,pion=t,"+"\n"+
"pihot = t, prQM = 5.5, prSM = 5.3, prQI=3.0, piguess = f,"+"\n"+
"pinit = t, pnstep = 5000, pnscale = 100,"+"\n"+"pnstat = 5, pnout = 5,
pnrst = 5, pioutc = t, pioutv = t, pnoutc = 5, pnoutv = 5,"+"\n"+
"msolute = t, nosa = 1, pichrg = t"+"\n"+'gfileOut = as-' +str(i)+ ".out,"+"\n"+
'gfileEnout = as-' +str(i)+ '.en,'+"\n"+'gfileInfo = as-' +str(i)+".info,"+"\n"+
'gfileStart = init' +str(i)+ ".in,"+"\n"+'gfileRst = as' +str(i)+ ".rst,"+"\n"+
'gfileTraj = as' +str(i)+ ".traj,"+"\n"
+'gfileVeloc = as' +str(i)+ ".vel,"+"\n"+'gfileQmen = as' +str(i)+ '.qmen'+"\n"+"&end ")
f.close()
The above given code produces right output but I want a way to read myfile.txt and change the values of last eight variables as mentioned above and then use this file to create ten new files.