Python 3 – File writelines() Method
In Python, we have various file methods that can be used to work with files. One of these methods is the writelines()
method. This method is used to write a sequence of strings to a file.
Syntax
The syntax for the writelines()
method is as follows:
file.writelines(iterable)
Here, file
is the file object that you want to write to, and iterable
is the sequence of strings that you want to write to the file. The iterable
can be a list or a tuple or any other iterable object.
Parameters
The writelines()
method takes only one parameter, which is an iterable sequence of strings. Each string in the sequence is written to the file as a separate line.
Return Value
The writelines()
method does not return any value. It simply writes the sequence of strings to the file.
Example 1 – Writing a List to a File using writelines() Method
In this example, we will use the writelines()
method to write a list of strings to a file.
# Open file in write mode
file = open("test.txt", "w")
# List of strings
lines = ["Hello World\n", "Python Programming\n", "File Handling\n"]
# Writing the lines to the file
file.writelines(lines)
# Close the file
file.close()
In the above code snippet, we first opened a file test.txt
in write mode using the open()
function. We then defined a list of strings lines
that we want to write to the file. Finally, we used the writelines()
method to write the entire list of strings to the file, which we then closed using the close()
method.
Example 2 – Writing a Tuple to a File using writelines() Method
In this example, we will use the writelines()
method to write a tuple of strings to a file.
# Open file in write mode
file = open("test.txt", "w")
# Tuple of strings
lines = ("Hello World\n", "Python Programming\n", "File Handling\n")
# Writing the lines to the file
file.writelines(lines)
# Close the file
file.close()
In the above code snippet, we first opened a file test.txt
in write mode using the open()
function. We then defined a tuple of strings lines
that we want to write to the file. Finally, we used the writelines()
method to write the entire tuple of strings to the file, which we then closed using the close()
method.
Example 3 – Appending a List to a File using writelines() Method
In this example, we will use the writelines()
method to append a list of strings to an existing file.
# Open file in append mode
file = open("test.txt", "a")
# List of strings
lines = ["More Text\n", "Python is fun\n"]
# Writing the lines to the file
file.writelines(lines)
# Close the file
file.close()
In the above code snippet, we first opened a file test.txt
in append mode using the open()
function. We then defined a list of strings lines
that we want to append to the file. Finally, we used the writelines()
method to append the entire list of strings to the file, which we then closed using the close()
method.
Example 4 – Writing a List as a Single String to a File using writelines() Method
In this example, we will use the writelines()
method to write a list of strings as a single string to a file by replacing each newline character with a single space character.
# Open file in write mode
file = open("test.txt", "w")
# List of strings
lines = ["Hello World\n", "Python Programming\n", "File Handling\n"]
# Writing the lines to the file
file.write(" ".join(lines))
# Close the file
file.close()
In the above code snippet, we first opened a file test.txt
in write mode using the open()
function. We then defined a list of strings lines
that we want to write to the file as a single string. Finally, we used the join()
method to join all the strings in the list with a space character and write the resulting string to the file using the write()
method. We then closed the file using the close()
method.
Conclusion
In this article, we learned about the writelines()
method in Python. We saw how to use thismethod to write a sequence of strings, such as a list or a tuple, to a file. We also learned that the writelines()
method does not return any value and that it writes each string in the sequence as a separate line in the file.
We also looked at some examples of using the writelines()
method to write various sequences of strings to a file, such as a list, a tuple, or a string. We also saw how to append a list of strings to an existing file.
By now, you should have a good understanding of how to use the writelines()
method to write sequences of strings to a file in Python. This method is an essential tool when working with files and can save you time and effort when you need to write a large number of strings to a file.
So go ahead and use the writelines()
method in your next file-handling project and see how it can simplify your file-writing tasks.