'simple approach to create a template file to be changed as needed

I have been trying to get into python programming for a bit but cannot find the right moment or project. Now I may have! I am trying to generate multiple files that are used as input for a DFT (density functional theory) software (quantum espresso) and I would like to use python for this.

An input file looks like this:

&control
 calculation         = 'scf'
 restart_mode        = 'from_scratch'
 prefix              = 'pto'
 disk_io             = 'low'
 tstress             = .true.
 tprnfor             = .true.
 etot_conv_thr       = 1.e-6
 forc_conv_thr       = 1.e-5
 verbosity           = 'high'
 pseudo_dir          = '../pp/'
 outdir              = 'pto'
/

&system
 ibrav               = 6
 celldm(1)           = 7.37934
 celldm(3)           = 1.06428
 nat                 = 5
 ntyp                = 3
 ecutwfc             = 75.
/

&electrons
 diagonalization     = 'ppcg'
 mixing_beta         = 0.1
 mixing_mode         = 'plain'
 conv_thr            = 1.e-10
/

ATOMIC_SPECIES
 Pb  207.2   Pb.upf
 Ti   47.867 Ti.upf
 O    15.999  O.upf

ATOMIC_POSITIONS {crystal}
Pb  0.0    0.0    z1
O   0.5    0.5    z2 
Ti  0.5    0.5    z3
O   0.0    0.5    z4
O   0.5    0.0    z5

K_POINTS {automatic}
3 3 3

What I would like to change is the z1, z2, z3, z4 and z5 parts in the atomic positions section. My idea was to create a file like this to be used as a template, use it as input for the python script, search for z1 (and also the rest of the zX) and replace it with the value I need. Save to a new file and repeat as needed.

The trouble is, I don't know how to approach this. I know how to generate the zZ values and that's about it.

Is there a simple way to approach this?

Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source