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

结果:
python中,循环写入_第1张图片

你可能感兴趣的:(Python)