Python File write() 方法
概述
write()方法用於向文件中寫入指定字符串。
在文件關閉前或緩衝區刷新前,字符串內容存儲在緩衝區中,這時你在文件中是看不到寫入的內容的。
語法
write() 方法語法如下:
fileObject.write( [ str ])
參數
str --要寫入文件的字符串。
返回值
該方法沒有返回值。
實例
以下實例演示了write() 方法的使用:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打开文件 fo = open("test.txt", "w") print "文件名为: ", fo.name str = "本教程" fo.write( str ) # 关闭文件 fo.close()
以上實例輸出結果為:
文件名为: test.txt
查看文件內容:
$ cat test.txt 本教程