Python writelines
In this post, you will learn about the writelines() method of the Python programming language.
Python provides an inbuilt method, writelines() to write a sequence of strings to the file. The sequence can be any iterable item representing the strings.
Syntax of writelines()
file. writelines(sequence)
Here, a sequence can be any iterable object producing strings, typically a list of strings. This method does not return any value. Where the written text will be inserted depends upon the file mode and stream position. If the file mode is "a", the text will be appended at the current file stream position, by default at the end of the file. And if the file mode is "w", the file record will be discharged before the texts will be inserted at the current file stream position, default 0.
Python writelines() program
Here is a simple example of Python writelines() method.
# uses of writelines() method with example
# creating a file
txt_file = open("demo.txt", "w")
# writing list of the strings to file
txt_file.writelines(["Quiz Competition - ","Winning candidates\n"])
# list of strings
str_list = ["Priska ","Abhishek ","John ","Henry "]
# writing list of the strings to file
txt_file.writelines(str_list)
# closing the file
txt_file.close()
# reading and printing text
txt_file = open("demo.txt", "r")
print(txt_file.read())
# closing the file
txt_file.close()
When we run the above program, it produces the following result:
Quiz Competition - Winning candidates
Priska Abhishek John Henry
Python writelines() program
This method can also be used to append text at the end of the existing content.
# uses of writelines() method with example
# opening a file in read write mode
txt_file = open("demo.txt", "a")
# writing list of the strings
# at the end of file
txt_file.seek(0,2)
txt_file.writelines(["\nChess Competition - ","Winning candidates\n"])
# list of strings
str_list = ["Nishu ","Prince"]
# writing list of the strings to file
txt_file.writelines(str_list)
# closing the file
txt_file.close()
# reading and printing complete file
txt_file = open("demo.txt", "r")
print(txt_file.read())
# closing the file
txt_file.close()
When we run the above program, it produces the following result-
Quiz Competition - Winning candidates
Priska Abhishek John Henry
Chess Competition - Winning candidates
Nishu Prince
Related Articles
Remove character from string Python
Python remove punctuation from string
Count vowels in a string Python
Count consonants in a string Python
Python multiline string
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python compare two lists
Insert data in MySQL database from an HTML form using Django
Alphabet pattern programs in Python