Python File writelines() 方法
概述
writelines() 方法用於向檔中寫入一序列的字串。
這一序列字串可以是由迭代對象產生的,如一個字串列表。
換行需要制定換行符 \n。
語法
writelines() 方法語法如下:
fileObject.writelines( [ str ])
參數
-
str -- 要寫入檔的字串序列。
返回值
該方法沒有返回值。
實例
以下實例演示了 writelines() 方法的使用:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打開檔 fo = open("test.txt", "w") print "檔案名為: ", fo.name seq = ["IT研修 1\n", "IT研修 2"] fo.writelines( seq ) # 關閉檔 fo.close()
以上實例輸出結果為:
檔案名為: test.txt
查看檔內容:
$ cat test.txt IT研修 1 IT研修 2