Introduction

As we all know that file is a collection of text that keeps the records of any event. File handling is essential for programmers as it allows user to perform operations on file such as read, write, append etc. File handling in python is super easy and it is important in any programming language because it stores the text data that can be used by programmers or program. The basic steps in file handling are open a file, perform operations and close the file. Python treats file as text or binary. Each line of code is terminated with a special character called EOL(End Of Line) characters such as comma, new line etc.

 

Opening a file in python

To access the file or to operate on file we need to read that file first. Python has  open( )  function that is used to open a file. For demonstration I will be using Jupyter Notebook. A file named as "student.txt" is located in current working directory.

Syntax: 

open(filename, mode)

filename → Name of  the file that needs to be accessed

mode → Mode in while file is opened

Let's take an example

file = open("student.txt","r")
print(file.name)

Output:

student.txt

This shows that file is opened successfully in read mode and can only be used for reading purpose.

 

Modes of opening file in python

<r> ⇒ open file in read mode only

<rb> ⇒ open file in (read + binary) mode

<r+> ⇒ open file in (read + write) mode

<rb+> ⇒ open file in (read + write + binary) mode

<w> ⇒ open file in write mode

<wb> ⇒ open file in (write + binary) mode

<w+> ⇒ open file  in (read + write) mode

<wb+> ⇒ open file in (read + write + binary) mode

<a> ⇒ open file in append mode

<ab> ⇒ open file in (append + binary) mode

<a+> ⇒ open file in (append + read) mode

<ab+> ⇒ open file in (append + binary + read) mode

 

Closing a file in python

Once a file is opened it must be closed after operating in file. Python has  close()  function that is used to close the file.

Syntax:

file_object.close()

Let's take an example

file = open("student.txt","r")
print(file.name)
file.close()
print(file.closed)

Output:

student.txt
True

Once the file is closed, we cannot perform any operations in file.

 

Writing in a file

To write in file python has  write()  function. Also to peform write operation file should be opened in write mode.

Syntax:

file_object.write(string)

Let's take an example

file = open("student.txt","w")
file.write("File handling in python is super easy.")
file.close()

To see the contents written in a file, we use  readline() or readlines()  function.

with open("student.txt") as f:
    print(f.readline())

Output:

File handling in python is super easy.

 

Appending in file

Python has  append()  function to append contents in a file. For this file should be open in append mode.

file = open("student.txt","a")
file.write("File handling is important.")
file.write("Programming with python is fun.")
file.close()

To display contents of file we need to open file in read mode

read_file = open("student.txt", "r")
for line in read_file:
    print(line)
read_file.close()

Output:

File handeling in python is super easy.File handling is important.Programming with python is fun.

 

Renaming a file

Renaming file need  rename()  function of os module

import os

os.rename("student.txt", "abc.txt")

The file named as "student.txt" is changed to "abc.txt"

with open("abc.txt", "r") as f:
    lines = f.readlines()
for line in lines:
    print(line)

Output:

python is easy.

python is cool.

Codefires is growing.

 

Removing a file

To remove file from system we need to use  remove()  function from os module.

os.remove("abc.txt")

Thus, file named "abc.txt" is removed from the system. If we want to verify it, we can do following,

try:
    with open("abc.txt") as f:
        print(f.readlines())
except:
    print("File doesnot exists")

Output:

File doesnot exists

 

Conclusion

File handling seems to be very useful for any programming language. With python, file handling is easy to peform. Files allows to keep records of big projects that assist programmers for better management of the project. Hence, file handling is very essential in python.

Happy Learning :-)