1
name email phonenumber
eric example@gmail.com 01381031
tuna tuna@gmail.com 1571051

above is the personal_details.txt

def personal(userid):
    print("Modify personal details")
    newemail = input("Enter your new email: ")
    newphone = input("Enter your new phone:")
    with open("personal_details.txt", "r+") as file:
        for line in file:
            line = line.split()
            if userid == line[0]:
                file.seek(0)
                file.write(" " + newemail + " " + newphone)
                file.truncate()

I am creating profile system which users can modify the details. I wanna overwrite the text in line[2] and line[3] of the files but it keeps overwriting the whole files. Im quite new to python so I really need some helps to figure out this problem.

Justin Koo
  • 11
  • 1
  • `file.seek(0)` sets the reference point to the beginning of the file instead of the line. – DeGo May 08 '21 at 11:24

1 Answers1

0

First copy the text to a new variable and use .replace to replace the desired words and then you can override the textfile