Python file writelines – Python File writelines() Method with Examples

Files In Python:

Python writelines to file: A file is a piece of data or information stored on a computer’s hard drive. You’re already familiar with a variety of file kinds, including music, video, and text files. Manipulation of these files is trivial with Python. Text files and binary files are the two types of files that are commonly used. Binary files contain binary data that can only be read by a computer, whereas text files include plain text.

For programmers and automation testers, Python file handling (also known as File I/O) is a crucial topic. Working with files is required in order to write to or read data from them.

In addition, if you didn’t know, I/O activities are the most expensive techniques via which software might fail. As a result, when implementing file processing for reporting or any other reason, you should proceed with caution. The construction of a high-performance application or a robust solution for automated software testing can benefit from optimizing a single file activity.

File writelines() Method in Python:

Python file writelines: The writelines() function in Python is used to write a list of strings (multiple strings or items of a list) to a file.

In which the texts will be inserted depending on the file mode and stream position.

“a”: The texts will be added at the current file stream position, which is often at the end of the file by default.

“w”: The file will be emptied before the texts are inserted at the current file stream position, which is 0 by default.

Syntax:

file.writelines(list)

Parameters

list: This is the list of texts or byte objects to be inserted.

Return Value:

This method doesn’t return anything.

File writelines() Method with Examples in Python

Example1

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in append mode. In this case, we’re simply appending/adding the contents into the file.
  • Write some random lines to the file by passing lines in the form of a list as an argument to the writelines() function.
  • Close the given file using the close function.
  • Open the file in read mode. In this case, we’re simply reading the contents of the file.
  • Read the contents of the file using the read() function.
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in append mode. In this case, we're simply appending/adding the contents into the file.
gvn_file = open(givenFilename, 'a') 
# Write some random lines to the file by passing lines in the form of list as an
# argument to the writelines() function.
gvn_file.writelines(["good morning btechgeeks\n", "welcome to python coding platform\n"])
# Close the given file using the close function
gvn_file.close()

# Open the file in read mode. In this case, we're simply reading the contents of the file.
gvn_file = open(givenFilename, 'r') 
# Read the contents of the file using the read() function
print(gvn_file.read())

Output:

Hello this is btechgeeks 
good morning btechgeeks
welcome to python coding platform

Original File Content:

Hello this is btechgeeks

File Content after adding two more lines:

Hello this is btechgeeks 
good morning btechgeeks
welcome to python coding platform

Example2: without giving line breaks(\n)

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in append mode. In this case, we're simply appending/adding the contents into the file.
gvn_file = open(givenFilename, 'a') 
# Write some random lines to the file by passing lines in the form of list as an
# argument to the writelines() function.
gvn_file.writelines(["good morning btechgeeks", "welcome to python coding platform"])
# Close the given file using the close function
gvn_file.close()

# Open the file in read mode. In this case, we're simply reading the contents of the file.
gvn_file = open(givenFilename, 'r') 
# Read the contents of the file using the read() function
print(gvn_file.read())

Output:

Hello this is btechgeeks 
good morning btechgeekswelcome to python coding platform

Google Colab Images:

Files and Code: