在.txt文件中,遍历每一行,将特定内容进行修改,如例子中将jpg修改为png,因为分割中的mask常为png格式
gt_path_1 = r'C:\Users\12736\Desktop\jpg2png\jpg.txt' # 被替换的.txt
gt_path_2 = r'C:\Users\12736\Desktop\jpg2png\png.txt' # 替换后的.txt
s = [] # 存储
f1 = open(gt_path_1,'r',encoding= 'UTF-8')
for line in f1:
if 'jpg' in line:
line_s = line.replace('jpg','png') # 将全部jpg替换为png
s.append(line_s)
f1.close()
f2 = open(gt_path_2,'w',encoding= 'UTF-8')
for line in s:
f2.writelines(line) # 将替换后的写入新的.txt
f2.close()
python 3.9.1
Windos11
希望大家多多支持我