在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()
結果: