Python 文件 writelines() 方法

အကျယ်ပြု

以 "a" 打开文件行附加,然后添加文本列表来附加到该文件:

f = open("demofile3.txt", "a")
f.writelines(["See you soon!", "Over and out."])
f.close()
#open and read the file after the appending:
f = open("demofile3.txt", "r")
print(f.read())

အကျယ်ပြု အချက်အလက်

定义和用法

writelines() 方法将列表的项目写入文件。

文本插入的位置取决于文件模式和流位置。

"a":文本将插入当前文件流的位置,默认情况下插入文件的末尾。

"w":在将文本插入当前文件流位置(默认为 0)之前,将清空文件。

အသုံးပြုခြင်း

file.writelines(list)

ပါဝင်သည် အတိုင်း

ပါဝင်သည် ဖော်ပြ
list ထိန်းချုပ် အချက်အလက် အသုံးပြု အဖွဲ့

ပိုမို အကျယ်ပြု

အကျယ်ပြု

အထဲတွင် အပြင်းအထန် ပြင်ဆင် ခြင်း နှင့် အတူ အချက်အလက် ကို အပြင်းအထန် ပြင်ဆင် ခြင်း

f = open("demofile3.txt", "a")
f.writelines(["\nSee you soon!", "\nOver and out."])
f.close()
#open and read the file after the appending:
f = open("demofile3.txt", "r")
print(f.read())

အကျယ်ပြု အချက်အလက်