Python File writelines() Method – Tutorial with Examples

Python File writelines() Method

The writelines() method in Python is a built-in method used to write a sequence of strings to a file. It takes an iterable as an argument, where each item in the iterable is a string to be written to the file. If the file does not exist, it will be created. If it already exists, the contents of the file will be overwritten.

Syntax

file.writelines(iterable)

Return Value

The writelines() method does not return anything.

Examples

Example 1: Writing a list of strings to a file

In this example, we will use the writelines() method to write a list of strings to a file:

file = open("example.txt", "w")
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
file.writelines(lines)
file.close()

In this example, we opened a file “example.txt” in write mode and used the writelines() method to write the list of strings “lines” to the file. We then closed the file using the close() method.

Output:

No output

The writelines() method does not return anything, so there is no output to display.

Example 2: Writing a set of strings to a file

In this example, we will use the writelines() method to write a set of strings to a file:

file = open("example.txt", "w")
lines = {"Line 1\n", "Line 2\n", "Line 3\n"}
file.writelines(lines)
file.close()

In this example, we opened a file “example.txt” in write mode and used the writelines() method to write the set of strings “lines” to the file. We then closed the file using the close() method.

Output:

No output

As with the previous example, the writelines() method does not return anything.

Example 3: Writing to a file using a loop

In this example, we will use a loop to write data to a file:

file = open("example.txt", "w")
for i in range(5):
    file.write("Line " + str(i+1) + "\n")
file.close()

In this example, we opened a file “example.txt” in write mode and used a loop to write five lines of data to the file. We then closed the file using the close() method.

Output:

No output

As with the previous examples, the writelines() method does not return anything.

Use Cases

The writelines() method is commonly used in situations where you need to write a sequence of strings to a file. It can be useful when you have a large amount of data to write to a file, as it allows you to write multiple lines of data in a single method call, rather than calling the write method for each line of data. It can also be useful when working with a data structure that contains multiple strings, such as a list or set.

One possible use case for the writelines() method is when working with log files. If your program generates log data that needs to be saved to a file, you can use the writelines() method to write the data to the file. This can make it easier to review the log data later on, as all of the data will be contained in a single file.

Conclusion

The writelines() method in Python is a useful method for writing a sequence of strings to a file. It takes an iterable as an argument and can be used to write lists, sets, or other iterables that contain strings. The method does not return anything, so there is no output to display. The writelines() method can be useful in situations where you need to write a large amount of data to a file, such as when working with log files.

Leave a Reply

Your email address will not be published. Required fields are marked *