Reading and Writing to text files in Python

Like other languages, Python provides some inbuilt functions for reading, writing, or accessing files. Python can handle mainly two types of files. The normal text file and the binary files.

For the text files, each lines are terminated with a special character '\n' (It is known as EOL or End Of Line). For the Binary file, there is no line ending character. It saves the data after converting the content into bit stream.

In this section we will discuss about the text files.

File Accessing Modes

Sr.No Modes & Description


It is Read Only mode. It opens the text file for reading. When the file is not present, it raises I/O Error.



This mode for Reading and Writing. When the file is not present, it will raise I/O Error.



It is for write only jobs. When file is not present, it will create a file first, then start writing, when the file is present, it will remove the contents of that file, and start writing from beginning.



It is Write and Read mode. When file is not present, it can create the file, or when the file is present, the data will be overwritten.



This is append mode. So it writes data at the end of a file.



Append and Read mode. It can append data as well as read the data.

Now see how a file can be written using writelines() and write() method.

Example code

Live Demo

#Create an empty file and write some lines
line1 = 'This is first line. \n'
lines = ['This is another line to store into file.\n',
   'The Third Line for the file.\n',
   'Another line... !@#$%^&*()_+.\n',
   'End Line']
#open the file as write mode
my_file = open('file_read_write.txt', 'w')
my_file.writelines(lines) #Write multiple lines
print('Writing Complete')


Writing Complete

After writing the lines, we are appending some lines into the file.

Example code

Live Demo

#program to append some lines
line1 = '\n\nThis is a new line. This line will be appended. \n'
#open the file as append mode
my_file = open('file_read_write.txt', 'a')
print('Appending Done')


Appending Done

At last, we will see how to read the file content from the read() and readline() method. We can provide some integer number 'n' to get first 'n' characters.

Example code

#program to read from file
#open the file as read mode
my_file = open('file_read_write.txt', 'r')
print('Show the full content:')
#Show first two lines
print('First two lines:')
print(my_file.readline(), end = '')
print(my_file.readline(), end = '')
#Show upto 25 characters
print('\n\nFirst 25 characters:')
print(, end = '')


Show the full content:
This is first line. 
This is another line to store into file.
The Third Line for the file.
Another line... !@#$%^&*()_+.
End Line

This is a new line. This line will be appended. 

First two lines:
This is first line. 
This is another line to store into file.

First 25 characters:
This is first line. 

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started