在python中,实现循环写入的功能:
txt_file = open("D:/文本文档.txt", "a", encoding="utf-8") # 将w换成 a
案例:
for temp in image_name:
txt_file = open("D:/文本文档.txt", "a", encoding="utf-8") # 以写的格式打开先打开文件
if temp.endswith(".jpg"):
txt_file.write(temp.replace('.jpg',''))
txt_file.write("\n")
txt_file.close()
结果: