Overview
The Python file Writelines () method is used to write a sequence of strings to a file.
This sequence string can be generated by an iterative object, such as a list of strings.
Line breaks need to be made with a newline character \ n.
Grammar
The Writelines () method syntax is as follows:
Fileobject.writelines (iterable)
Parameters
return value
The method has no return value.
Instance
The following example demonstrates the use of the Writelines () method:
#!/usr/bin/python3# Open the file fo = open ("Test.txt", "W", encoding= "Utf-8") print ("File name:", fo.name) seq = ["Rookie tutorial 1\n", "Rookie Tutorial 2"]fo . Writelines (SEQ) # Close File Fo.close ()
The result of the above example output is:
File name is: test.txt
To view the contents of a file:
$ cat Test.txt Rookie Tutorial 1 rookie Tutorial 2
Python file Writelines () method