File Handling In Python

File Handling In Python

This blog covers the creating, reading, updating and deleting of files in python. Learning the handling files is as important as learning how to send requests in a web application.

The key function in file handling in python is the open() function. It takes two arguments; the file name and the mode. The are four modes involved in file handling.
* "r" opens the file for reading and throws an error if the file doesn’t exist.
* "a" append mode creates file if file doesn’t exist, adds items to a file.
* "w" write mode, opens file for writing and creates the file if it does not exist.
* "x" creates the specified file, returns an error if the file exist.

in addition you can specify how the file can be handled, as a binary file or a text file…
* "t" text, this is the default value
* "b" binary mode, e.g., the handling of images and other binary files.


Opening a file for reading is actually enough to specify the name of the file,

  myfile= open("blog_file.txt")

the above is also same as writing:

  myfile = open("blog_file.txt", "rt")

This is basically obvious since the "r" and the "t" are the default values of the open method in file handling. f is just a variable to store the file, just as we store data types in python.

If we had the file “blog_file” containing the following

Hi there,
Figured you just named me blog_file… good choice of name by the way
use me responsibly 😅

The open function returns a file object named myfile in our case with access to the read() method for accesssing the contets and can be displayed by:


To read only parts of the file, you can pass an argument with where you want to read, e.g.,

  print( which would output “name by the way” it being the 3rd line.

Writing To an Existing File:

open("myfile.txt", "a")
myfile.write("This is new line to give you company 🙂 ")

When we use "w" instead of the "a" wee overide the contents of the file, this is serious {{The entire file 😠}}.

The other that I have not covered yet is creation using the "x" mode. This checks if the file created exists, if not then an empty file is created. Otherwise, an error is thrown.

Deleting a File 📥:

Deleting a file requires importation of the os module and thus deleting using the os.remove() method.

import os

on the other hand you can delete a folder using

import os

Another tip you can use to avoid errors is checking for conditions before actually deleting the file or opening a non existing one using the open() funciton, this is one of the reasons as to why we get to encourage the use of the "x” mode for creating files, since it throws an error if the file exists 😜,

import os
if os.path.exists("path_to_file_am_looking_for.txt"):
return "I liked the blog *.'"
return "I still liked the blog *.'"

A clap a time does a clap ++ operation to the blog’s claps variable 😉. Till next time 😃


Passionate about web development, design, and network security. With a minor in interaction design, I aspire toward a career that will allow me to channel my creativity through crafting beautiful software and engaging experiences. I love learning new stuff and also mentoring people. I am currently learning node and react as I also polish my skills on the ones I know. When I'm not working, I love gaming, talking to new people and viewing awesome gadgets on the web.