[Text file reading and writing] A python program that opens a text file, reads the first ten lines of data and adds a new line of data at the end of the file

The problem description is as follows:

Open the text file datah.txt, (the content of the text file is shown in the figure below) read the first ten lines of data, and then add "1.8 80 fat" at the end of the file.

 

The program code is as follows:

fname = "dataH.txt"
fo = open(fname, "r+")
i = 0
for line in fo.readlines():
    if i == 10:
        break
    print(line)
    i = i + 1
ls = ["1.8 80 fat"]
fo.writelines(ls)
fo.seek(2)
fo.seek(0)
for line in fo.readlines():
    print(line)
fo.close()

The result of the program running is as follows:

 

 

 Friends who see this, don’t forget to like it before leaving!

Follow bloggers to learn more about python programming knowledge!

Guess you like

Origin blog.csdn.net/qq_59049513/article/details/122582330