Difference between write() and writelines() function in Python
Last Updated :
22 Sep, 2021
In Python, there are many functions for reading and writing files. Both reading and writing functions work on open files (files opened and linked via a file object). In this section, we are going to discuss the write functions to manipulate our data through files.
write() function
The write() function will write the content in the file without adding any extra characters.
Syntax:
# Writes string content referenced by file object.
file_name.write(content)
As per the syntax, the string that is passed to the write() function is written into the opened file. The string may include numbers, special characters, or symbols. While writing data to a file, we must know that the write function does not add a newline character(\n) to the end of the string. The write() function returns None.
Example:
Python3
file = open("Employees.txt", "w")
for i in range(3):
name = input("Enter the name of the employee: ")
file.write(name)
file.write("\n")
file.close()
print("Data is written into the file.")
Output:
Data is written into the file.
Sample Run:
Enter the name of the employee: Aditya
Enter the name of the employee: Aditi
Enter the name of the employee: Anil
writelines() function
This function writes the content of a list to a file.
Syntax:
# write all the strings present in the list "list_of_lines"
# referenced by file object.
file_name.writelines(list_of_lines)
As per the syntax, the list of strings that is passed to the writelines() function is written into the opened file. Similar to the write() function, the writelines() function does not add a newline character(\n) to the end of the string.
Example:
Python3
file1 = open("Employees.txt", "w")
lst = []
for i in range(3):
name = input("Enter the name of the employee: ")
lst.append(name + '\n')
file1.writelines(lst)
file1.close()
print("Data is written into the file.")
Output:
Data is written into the file.
Sample Run:
Enter the name of the employee: Rhea
Enter the name of the employee: Rohan
Enter the name of the employee: Rahul
The only difference between the write() and writelines() is that write() is used to write a string to an already opened file while writelines() method is used to write a list of strings in an opened file.
Similar Reads
Difference between modes a, a+, w, w+, and r+ in built-in open function? Understanding the file modes in Python's open() function is essential for working with files effectively. Depending on your needs, you can choose between 'a', 'a+', 'w', 'w+', and 'r+' modes to read, write, or append data to files while handling files. In this article, we'll explore these modes and
4 min read
What is the Difference between Interactive and Script Mode in Python Programming? Python is a programming language that lets you work quickly and integrate systems more efficiently. It is a widely-used general-purpose, high-level programming language. It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines o
7 min read
Python - Difference between json.dump() and json.dumps() JSON is a lightweight data format for data interchange which can be easily read and written by humans, easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json. Note: For more information, refer to
2 min read
Statement, Indentation and Comment in Python Here, we will discuss Statements in Python, Indentation in Python, and Comments in Python. We will also discuss different rules and examples for Python Statement, Python Indentation, Python Comment, and the Difference Between 'Docstrings' and 'Multi-line Comments. What is Statement in Python A Pytho
7 min read
Difference Between Carriage Return (CR) and Line Feed (LF) ? How it is used in Different Operating System? While using text files in scripts on different operating systems, you may be concerned with different newline characters, which can also lead to confusion and file incompatibility. Two primary characters are used to denote the end of a line: Carriage Return (CR) and another command called Line Feed
7 min read