Python File write () method
Outline
write () method is used to write to the file specified string.
Before the file is closed or before the buffer is flushed, the contents of the string stored in the buffer, then you can not see in the document is written content.
grammar
write () method has the following syntax:
fileObject.write( [ str ])
parameter
str - the string to be written to the file.
return value
This method has no return value.
Examples
The following example demonstrates the write () method of use:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打开文件 fo = open("test.txt", "w") print "文件名为: ", fo.name str = "本教程" fo.write( str ) # 关闭文件 fo.close()
The above example output is:
文件名为: test.txt
Check file contents:
$ cat test.txt 本教程