I have several files and want to add some text to them (named as changed_1, changed_2, changed_3 and so on) in python. This is some lines of my files (but my files have thousands of rows):
$MeshFormat
2.2 0 8
$EndMeshFormat
$PhysicalNames
8
2 12 "back"
2 14 "Fault"
3 1 "volume_1"
$EndPhysicalNames
...
I want to add line/s exactly after the 8th line (3 1 "volume_1"). These lines should be also generated dynamically. I have another variable named as n_iteration. If n_iteration is 2, I want to add 3 2 "volume_2" exactly after the 8th line. If it is 3, I want to add 3 2 "volume_2" and 3 3 "volume_3" and so on. Finally I want to have my files changed and saved exactly like the input format but with this added lines (let's say n_iteration = 4):
$MeshFormat
2.2 0 8
$EndMeshFormat
$PhysicalNames
8
2 12 "back"
2 14 "Fault"
3 1 "volume_1"
3 2 "volume_2"
3 3 "volume_3"
3 4 "volume_4"
$EndPhysicalNames
...
I could only import all my files and sort them with the following code, but I was not successful in doing what I explained:
from glob import glob
all_files = glob('changed_*')
for i in all_files:
with open(str(i)) as lines:
...
In advance, I appreciate any help and feedback.