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

結果:
在這裏插入圖片描述

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