0

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.

3
  • So which part are you having trouble with? Finding the place to add the line? Deciding what to add? Actually adding the line? Making the string to add? Commented Nov 2, 2020 at 15:35
  • dear @wwii, to be honest. all of them. I am new in coding and python and do not know how to code what I want. Commented Nov 2, 2020 at 15:37
  • 1
    Welcome to SO. This isn't a discussion forum or tutorial. Please take the tour and take the time to read How to Ask and the other links found on that page. Invest some time with the Tutorial practicing the examples. It will give you an idea of the tools Python offers to help you solve your problem. - Searching with all of those questions should give you plenty of reading to get you started on each of those tasks. Commented Nov 2, 2020 at 15:38

2 Answers 2

2

You need to understand the difference between a file and its contents. You need to understand how to read in the contents of a file, and what can and cannot be done in writing to a file. In general, you can write to the end of a file, and you can write over a file, but you cannot insert into a file. You can create the effect of inserting by reading in the contents of the file, and writing it back out with the new content added in.

One approach: you read in the file line by line, so that you have a list of the lines. If you're always inserting your new data after the eighth line, you can now write out the contents one by one, then after you have written out the eighth line, write out your new data, then continue with the rest of the contents you read in at the start.

Sign up to request clarification or add additional context in comments.

Comments

1

This is basically the code to @Bens comment, except that i doesn't count until the eigth line when writing, but inserts the stuff you want to add to the read list and then writes everything to a file

with open("test.txt", "r") as f:
    data = f.readlines()


n_iteration = 2

for i in range(n_iteration,0,-1):
    data.insert(7, ('3 ' + str(i) +' "volume_' + str(i) + '"\n'))

with open("test_new.txt", "w") as f:
    for c in data:
        f.write(c)

3 Comments

Dear @Alexander. I do appreciate your solution. There are only two issues. How can I do it for a bunch of files rather than only one (I imported them using all_files = glob('changed_*'))? The result has the 8th line and new added ones. How can I get rid of the old one ore how can only one line after the 8th?
please check out some python bascis. really just check youtube youtube.com/watch?v=Uh2ebFW8OYM or something. I would create a function of the solution i provided to you and call it for every file you want to change. Instead of inserting (insert()) you can of course directly set the list elements to the desired values
Dear @ Alexander Riedel. Thanks for being that much supportive. I really appreciate your help.

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.