Latest web development tutorials

Python3 File writelines () method

Python3 File (File) method Python3 File (File) method


Outline

writelines () method is used to write a sequence of strings to the file.

This sequence string can be generated by the iteration object, such as a list of strings.

Wrap the need to develop a newline \ n.

grammar

writelines () method has the following syntax:

fileObject.writelines( [ str ])

parameter

  • str - the string to be written sequence file.

return value

This method has no return value.

Examples

The following example demonstrates the use truncate () method:

#!/usr/bin/python3

# 打开文件
fo = open("test.txt", "w")
print ("文件名为: ", fo.name)
seq = ["本教程 1\n", "本教程 2"]
fo.writelines( seq )

# 关闭文件
fo.close()

The above example output is:

文件名为:  test.txt

Check file contents:

$ cat test.txt 
本教程 1
本教程 2

Python3 File (File) method Python3 File (File) method