5 Essential Methods for File Handling in Python

read(),readline(), readlines(), write(), writelines() methods in file handling in Python

Why do we need file handling in Python?

Usually, when we create a program and run it, the console screen displays the output but the console screen has a volatile memory which means it has limited memory to accumulate a huge amount of data. So to achieve this, the console screen removes the old data and shows a new one which causes the loss of data.

As you can see, there is a program to generate numbers from 1 to 175001 but the console screen is showing output from 12746 to so on. The numbers before 12746 were generated, but due to the unavailability of space, the Console screen had to remove old data to show new data, resulting in the loss of data.

This problem is solved by the concept of file handling in Python. So the question is how does it help the user or what solution it offers.

As the name(file handling in Python) suggests, Python helps us to create a program that interacts with a file in a computer and stores all the data directly into a file. Whatever we learn in Python (if-else, loops, list, string, etc), we do the same thing but the only difference is that our output gets generated in the file not on the console screen.

How to perform file handling in Python:

Performing file handling in Python is very easy and convenient to learn and perform as compared to other languages.

Hence, a file operation can be done in the following order:

  1. Open a file with access mode
  2. Read or write – Performing operation
  3. Close the file

File operations

1. Open(): To open a file we use open() function to open a file with different access modes.

Access Modes

A. x: It is used to create a file. It throws an error if the file already exists.

2. Read or Write (Performing Operations with other access modes): We can perform both read and write operations in a file.

B. ‘a’: It opens a file in append mode only. It adds the data to the file if it exists.

C. ‘r’: It opens a file in read mode only. It reads the content of a file and displays it on the console screen. It does not allow to make any changes in the file.

To read the content of a file we have 3 methods to use, which are:
a. read(): This method reads the entire file content.

b. readline(): This method reads a single line from the file.

c. readlines(): This method reads all the lines from the file and returns them as a list of strings.

D. w: It opens a file in write mode only. It overwrites a file’s content, meaning it will add new data and delete the old data.

a. write(): This method helps the user to write content in a file in string data type.

b. writelines(): This method helps the user to write content in a file. It writes a list of lines in a file.

E. r+: It opens the file to read and write both. The file pointer exists at the beginning of the file.
F. w+: It opens the file to write and read both. It is different from r+.
G. a+: It opens a file to append and read both. The file pointer remains at the end of the file.

3. Close(): To close the file after performing the necessary operations.

To perform the operations open and close, Python also offers us one more technique which is “with statement”. This statement works with indentation which helps Python to understand the structure of operations to perform and then close the file automatically.

Want to learn more about file handling in Python and its syntaxes,
Don’t forget to follow us on Facebook, Instagram, and Linkedin

Interested in knowing, what else can be done with Python language?
Register yourself and join the free workshop conducted by ConsoleFlare.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top