Member-only story

How to Read and Write a File in Python ?

A quick guide to file operations in python

Bijen Adhikari
3 min readMay 2, 2022
source: https://logos-world.net/python-logo/

Open/ Close

The syntax for opening a file operation is :

There are two operations associated with file. We use open to create a file descriptor. Then we have to close the file to prevent memory leakage.

But closing the file manually is tedious process. And prone to error.

In the above example the file is opened but never closed. Because once the exception happens, the program will exit.

--

--

Bijen Adhikari
Bijen Adhikari

Written by Bijen Adhikari

Hi, I am Bijen from Nepal I write about Electronics, Electrical, Programming, Fitness. Also, I am a certified SSI Master Trainer.

Responses (1)