python中,循环写入

在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()

结果:
在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章