0

I want to insert a line into file "original.txt" (the file contains about 200 lines). the line neds to be inserted two lines after a string is found in one of the existing lines. This is my code, I am using a couple of print options that show me that the line is being added to the list, in the spot I need, but the file "original.txt" is not being edited

with open("original.txt", "r+") as file:
    lines = file.readlines()    # makes file into a list of lines
    print(lines)                #test
    for number, item in enumerate(lines):
        if testStr in item:
            i = number +2
            print(i)            #test
            lines.insert(i, newLine)
            print(lines)        #test
            break
file.close()

I am turning the lines in the text into a list, then I enumerate the lines as I look for the string, assigning the value of the line to i and adding 2 so that the new line is inserted two lines after, the print() fiction shows the line was added in the correct spot, but the text "original.txt" is not modified

0

2 Answers 2

4

You seem to misunderstand what your code is doing. Lets go line by line

with open("original.txt", "r+") as file:       # open a file for reading
    lines = file.readlines()                   # read the contents into a list of lines
    print(lines)                               # print the whole file
    for number, item in enumerate(lines):      # iterate over lines
        if testStr in item:                    
            i = number +2                      
            print(i)            #test
            lines.insert(i, newLine)           # insert lines into the list
            print(lines)        #test
            break                              # get out of the look
file.close()                                   # not needed, with statement takes care of closing

You are not modifying the file. You read the file into a list of strings and modify the list. To modify the actual file you need to open it for writing and write the list back into it. Something like this at the end of the code might work

with open("modified.txt", "w") as f:
    for line in lines: f.write(line)
Sign up to request clarification or add additional context in comments.

Comments

1

You never modified the original text. Your codes reads the lines into local memory, one at a time. When you identify your trigger, you count two lines, and then insert the undefined value newLine into your local copy. At no point in your code did you modify the original file.

One way is to close the file and then rewrite it from your final value of lines. Do not modify the file while you're reading it -- it's good that you read it all in and then start processing.

Another way is to write to a new file as you go, then use a system command to replace the original file with your new version.

Comments

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.