python|Batch append content to the last line of txt

Original txt content:
insert image description here
The last line of all txt in the folder is appended: 33 0.505794 0.947476 0.166326 0.055935. As shown in the picture:
insert image description here

import glob
import os
import shutil

Path=r"C:\Users\YUXIAOYANG\Desktop\labels"
for root,dirs,filename in os.walk(Path):
    for i in range(len(filename)):
        with open(root+"\\"+filename[i],'a') as f:
            f.write('33 0.505794 0.947476 0.166326 0.055935'+'\n')

Knowledge points:
insert image description here
Reference blog: Python: Adding and reading to TXT files with existing content

Guess you like

Origin blog.csdn.net/weixin_44769034/article/details/125378727