Python writelines() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

A file is a collection of data or various information that is stored on a computer. We can easily edit these files using Python. Python may be used to open, read from, and write to these files. You can accomplish the task of opening, reading, and writing to these files using the Python writelines method. In simpler words, the Python writelines method enables you to simultaneously write a single or multi-lines of text or any kind of string to the given text file. By using a string as an item in a list, we may use the writelines function to write many strings into a file at once. In this article, we are going to deep dive into Python writelines method.

Syntax of Python writelines() Function

The syntax of the writelines() function in Python is mentioned below:

Syntax

The above syntax of writelines() method is used to write a list of strings or multiple texts to a file. If you want to add a single string to the file then it can also be achieved using the writelines() function.

The Python writelines function also accepts a string as an argument and the syntax of which is given as below:

As shown in the above syntax, the writelines() function also accepts a string as an argument and writes a string to the file.

Parameters of Python writelines() Function

As you can see in the syntax above there is a parameter present in the Python writelines method. Let's see what the Python writelines method accepts as a parameter.

Parameter

  • list_of_strings: The "list_of_strings" is a list that consists of various strings which are to be included in the file. OR
  • string: This is the string value that is to be included in the file.

Return Value of Python writelines() Function

There is no return value of the writelines() function in Python. As the Python writelines method writes a sequence of the string or the text to the specified file. That means the return value of the Python writelines function is None.

Working of Python writelines() Function?

As shown in the syntax above, the list of strings is passed to the writelines() function in Python and this list of strings is written to the opened file.

The writelines() method writes the data or the list of strings to the file depending on the mode of the file. Let's take a look at different modes of the file. If the file mode is "w", then the file will be emptied first and then the texts will be placed at the current file stream position. And, if the file mode is "a", then the list of strings or the texts will be inserted at the current file stream position.

Difference Between write() and writelines() Function

  • The write() method accepts a string as an argument and writes this string to the text file. Whereas, the writelines() method accepts an iterable i.e. a string or the list of strings as an argument and writes these strings to the text file. If you provide a list of strings to the write() method, then it will raise an exception.
  • The write() function displays the output but does not provide the newline character whereas the newlines() function displays the output and provides a new line character at the end of the string. Also, the writelines() method do not automatically insert a new line character after each item in the iterable i.e. we have to provide the newline characters or any required characters by ourselves.

The below example clearly explains the difference between the write and writelines functions in Python.

Example:

Output:

Now, let's take the same example but this time instead of using the write function, we'll see the effect of writelines function in Python.

Example:

Output:

The writelines() function does not throw any error as it can accept a list of strings. Also, as you can see we have not provided the newline character and therefore all the strings are printed in a line as they are assigned to the variables.

Uses of Python writelines() Function?

  • The writelines() function is used to add a list of strings to the text file. Here, the texts will be inserted depending on the file mode.
  • The writelines() function is also used to append a string of lines to a file and an example of the same is discussed below.

Examples of Python writelines() Function

Now, let's take some examples to understand the writelines() function.

Example:

In the above example, first of all, a python file object is created by invoking the open method and the file named demofile.txt is opened. This method will either open the specified file if it already exists or it'll create a new file with the given name if the file is not present in the working directory. Now, the file mode is specified as "a" i.e. all the content of the file will not be erased and the new strings to the file will be added after the already present content in the file.

Now, to write the text strings to the file, we have used the writelines() function. The list of strings is provided as an argument to the writelines function. Before executing the program we have to close the file that was opened at the beginning of the program. Now, let's see how the output would look like:

Output:

Example:

In this example, we've used the "w" file mode while opening the file. Therefore, all the contents of the file will be erased and whatever strings or the list of strings present in the writelines function will be written to the demofile.txt.

Output:

Conclusion

  • The writelines() function enables you to simultaneously write a single or multi-lines of text or any kind of string to the given text file.
  • The writelines() function is used to write a single or list of strings simultaneously to a file.
  • Whereas the Python write method accepts an only a string as an argument and the same string is used to write to a file.
  • The return value of the Python writelines method is none i.e. it does not return any value.
  • The write() method accepts a string as an argument and writes this string to the text file. Whereas, the writelines() method accepts a string or the list of strings as an argument and writes these strings to the text file.