input_path = r"E:\datasets\WLKdata_1111\WLK_voc\ImageSets\Segmentation\val.txt" output_path = r"E:\datasets\WLKdata_1111\WLK_voc\ImageSets\Segmentation\val_no.txt" with open(input_path, "r", encoding="utf-8") as fin, open(output_path, "w", encoding="utf-8") as fout: for line in fin: fout.write(line.strip().replace(".tif", "") + "\n")