0

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.

1 Answer 1

1

str.format handles the inserts into the string to be written to each of the files.

# Write the files.
for i in range(1, 11):
   with open('file' +str(i)+ '.txt','w') as f:
       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-{index}.out,\n'
           'gfileEnout = as-{index}.en,\n'
           'gfileInfo =  as-{index}.info,\n'
           'gfileStart = init{index}.in,\n'
           'gfileRst =   as{index}.rst,\n'
           'gfileTraj =  as{index}.traj,\n'
           'gfileVeloc = as{index}.vel,\n'
           'gfileQmen =  as{index}.qmen\n'
           '&end ').format(index=i))

Note: The string contains {index} which is replaced by the value of i that range(1, 10) sets.

Edit: Re-done post due to misunderstanding the details of the question alerted by the 1st comment. Sorry.


Edit: Looked at need to read from file, so this may help.

template.txt:

&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-{index}.out,
gfileEnout = as-{index}.en,
gfileInfo =  as-{index}.info,
gfileStart = init{index}.in,
gfileRst =   as{index}.rst,
gfileTraj =  as{index}.traj,
gfileVeloc = as{index}.vel,
gfileQmen =  as{index}.qmen
&end

main script:

# Read template file.
with open('template.txt') as r:
    content = r.read()

# Write the files.
for i in range(1, 11):
   with open('file' +str(i)+ '.txt','w') as f:
       f.write(content.format(index=i))
Sign up to request clarification or add additional context in comments.

2 Comments

It's not working as asked, it is producing a1-a7 for first file, b1-b7 for second file and in the same way j1-j7 for last file. But I asked to just number the all eight mentioned variables with the same number which would be the number of the file. Also I have not asked adding any alphabets like a-z with the variables.
I misinterpreted details of the question. Sorry. The previous answer was so bad I scraped it and re-posted new code. Inform if I understood better this time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.