import os
root_dir = "DATASET/NEW/train"
target_dir = "ants_image"
imag_path = os.listdir(os.path.join(root_dir, target_dir))
label = target_dir.split("_")[0]
out_dir = "ants_label1"
for i in imag_path:
file_name = i.split(".jpg")[0]
with open(os.path.join(root_dir,out_dir,"{}.txt".format(file_name)),"w") as f:
f.write(label)
with open(os.path.join(root_dir,out_dir,"{}.txt".format(file_name)),"r") as f:
a=f.read()
print(a)
python写入TXT文件和读取TXT文件
最新推荐文章于 2024-05-30 15:12:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)