Python3 File writelines() 方法
概述
writelines()方法用於向文件中寫入一序列的字符串。
這一序列字符串可以是由迭代對象產生的,如一個字符串列表。
換行需要製定換行符\n。
語法
writelines() 方法語法如下:
fileObject.writelines( [ str ])
參數
str --要寫入文件的字符串序列。
返回值
該方法沒有返回值。
實例
以下實例演示了truncate() 方法的使用:
#!/usr/bin/python3 # 打开文件 fo = open("test.txt", "w") print ("文件名为: ", fo.name) seq = ["本教程 1\n", "本教程 2"] fo.writelines( seq ) # 关闭文件 fo.close()
以上實例輸出結果為:
文件名为: test.txt
查看文件內容:
$ cat test.txt 本教程 1 本教程 2