How to work with a text file in Python?


For creating, reading, updating, and removing files, Python includes a number of functions. When reading or writing to a file, the access mode determines the kind of operations that can be performed on the file.

Creating a file using Python

To create new files or open existing files, we can use the open() function. This function accepts two parameters: a string value representing the name of the file you need to create and a character representing the access mode of the file that is to be created. Following is the syntax of this function.

File = open("txt_file_name" ,"access_mode")

There are 6 access modes in python namely −

  • Read Only ('r') to open a text file for reading.

  • Read and Write ('r+') − Allows you to read and write to a file.

  • Write Only ('w') − Allows you to write to a file.

  • Write and Read ('w+') − Allows you to read and write to a file.

  • Append Only ('a') − Allows you to write to a file.

  • Append and Read ('a+') − Allows you to read and write to a file.

Example 1

Let’s look at an example to open the file in read only access mode.

file= open("example.txt", "r")
print("Example.txt opened successfully.")

Output

Example.txt opened successfully.

The file example.txt will be opened in read mode.

Example 2

Let’s look at example to open a file in read and write only mode which Allows you to read and write to a file.

file = open("example.txt", "r+")
print("Before adding text\n",file.read(20))
file.write("Hello World")
print("After adding text\n",file.read(40))
file.close()

Output

As we can see in the output generated below, "Hello World " is added to the file.

Before adding text 
This is example.txt file.
After adding text
This is example.txt file. Hello World

Example 3

Let us look at an example to open a file in write mode which allows you to write to a file.

file = open("example.txt", "w")
print("File opened in writing mode. ")

Output

The following is the output generated.

File opened in writing mode. 

Closing a file using Python

Python file method close() closes the opened file. A closed file cannot be read or written any more. Any operation, which requires that the file be opened will raise a ValueError after the file has been closed. Calling close() more than once is allowed.

Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.

Example

In this example, we will understand how the close() method works. We use this method to close a file after use. we simply call it along with the file handler at the end of the program. This is done so that the file does not get corrupted.

file = open('example.txt', 'r')    
file.close()
print( "file closed successfully ")

Output

The output obtained is as follows.

file closed successfully  

Updated on: 11-May-2023

845 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements