| by Arround The Web | No comments

Python File writelines() Method

While working in Python, we have to deal with small as well as large files for data manipulation and organization. Python provides various functions to handle files and perform operations on it. To write single or multiple lines of data into a Python file the “writelines()” method is used in Python.

This tutorial will present you with a detailed guide on the “file.writelines()” method in Python via the below contents:

What is the “file.writelines()” Method in Python?

In Python, the “file.writelines()” method is utilized to write a string sequence or list items to the file. Here, the sequence is any iterable object that generates the strings, typically a strings list. We can insert texts based on the following file mode:

  • The “a” mode is utilized to insert the text at the present file stream position/value which is the file end by default.
  • The “w” mode is utilized to insert the text at the “0” file stream position after emptying the current file system.

Syntax

file.writelines(list_of_strings or string)

 

Parameters

In the above syntax, the “list_of_strings” parameter specifies the multiple item or text that needs to be added to the file. We can also add single-string text to the file.

Return Value

This method retrieves no value.

Example 1: Writing Multiple Lines to the File Using “file.writelines()” Method

In the below code, the “open()” method opens the file in write/writing mode. Next, the “file.writelines()” method is used to write multiple lines of text by accepting the list of strings as an argument. Lastly, the file is closed using the “close()” method:

file = open("new.txt", "w")
file.writelines(["Hello", "\nWelcome to Python!"])
file.close()

 

As you can see, the multiple lines of text have been written to the file successfully:

Example 2: Appending Multiple Lines to the File Using “file.writelines()” Method

Here, the “open()” method is utilized for file opening in append mode. After that, the “file.writelines()” method is used to append multiple lines of string text to the particular file. After appending the file is closed using the “file.close()” method:

file = open("new.txt", "a")
file.writelines(["Linux Guide", "\nJava Guide"])
file.close()

 

The multiple lines of the file have been appended successfully:

Example 3: Appending and Reading Multiple Lines to the File Using “file.writelines()” Method

In the below code, we first append the multiple lines of strings to the file by using the “file.writelines()” method. After closing the file, the “file.read()” method is utilized for file reading:

file = open("new.txt", "a")
file.writelines(["\nLinux Guide", "\nJava Guide"])
file.close()
file = open("new.txt", "r")
print(file.read())

 

The multiple lines texts have been appended and read successfully:

Example 4: Difference Between “file.write()” Method and “file.writelines()” Method

In Python, the “file.write()” method and the “writelines()” method are used to write data to the file. A single string argument is required by the “write()” method to write to the file, whereas the “writelines()” method needs a list of string arguments. Then, write multiple lines to the file. Take the following examples to demonstrate the difference:

file = open("new.txt", "w")
file.write("Welcome\n")
file.write("to Python Guide!")
file.close()

 

As you can see, the multiple lines of string have been successfully written to the file by using the “file.write()” method twice in the program:

On the other hand, the “file.writelines()” method is used to write multiple lines of string by taking the element of the list as an argument:

file = open("new.txt", "w")
file.writelines(["Welcome\n", "to Python Guide!"])
file.close()

 

Multiple lines of text have been written to the file:

Conclusion

The “file.write()” method in Python is utilized to write the single or multiple string sequence or list item to the file. We can use the “file.writelines()” method with the “open()” function to open/launch the file in append/adding mode and write multiple lines. We can also read the multiple lines included in the file using the “file.read()” method. This guide delivered a detailed overview of the “file.writelines()” method using various examples.

Share Button

Source: linuxhint.com

Leave a Reply