Python/ref file writelines
来自菜鸟教程
<languages />
Python File 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())
定义和用法
The
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())