Mastering File Handling in Python: A Comprehensive Guide to Reading, Writing, and Manipulating Files

Gopal Khadka
3 min readAug 29, 2023

--

In Python, file handling is a built-in feature that allow you to manipulate the contents of a file. It doesn’t require any installation of libraries or package. You can append data, delete data, create file or replace content using built-in functions. You need this feature when you want to save some data after code execution or use the data from the file.

Opening And Closing File : Method 1

You have to first open the file and then close the file after doing required operations. If the file is opened and not closed, the file remains open and consumes your computer’s memory. So it is a must to close the file after opening it.

# Open the file in read mode
file = open("example.txt", "r")

# Close the file
file.close()

Opening And Closing File : Method

There is a more popular alternative method for handling the file without explicitly opening and closing the file. You can use with keyword to automatically close the file after doing operations.

with open("example.txt", "r") as file:
#code for file operations

This code opens a file named example.txt for reading purpose, as the second parameter “r” denotes read mode of the file. The as keyword gives the variable name for the file. Other modes will be discussed later. Using the with statement ensures that the file is properly closed, even if an exception occurs.

Reading Contents of File

with open("example.txt", "r") as file:
contents=file.read()
print(contents)

This code opens the text file in read mode. Inside the with block, contents of the file are read using read() method and then printed.

Writing Contents In File

text= "This is example text file."
with open("example.txt", "w") as file:
file.write(text)

This code opens the text file where “w” means write mode. Inside the with block, contents of the file are written using write() method by passing a string named text. This method only takes string as input. If a file named example.txt is not in the current folder, it creates a new file of the same name. If the file is present, it replaces the original content with the string.

Appending Contents To File

text= "This is example text file."
with open("example.txt", "a") as file:
file.write(text)

This code opens the text file, where “a” means append mode. Inside the with block, contents of the file are appended using write() method by passing a string named text. This method only takes string as input. Unlike the write mode, this mode appends the given content to the original content. If there is no file, it creates a file similar to write mode.

File Operations in Binary File

Like normal files, there are three modes for handling binary files:

  • rb mode: read content of binary files
  • wb mode: write in binary files
  • ab mode: append contents to binary files
with open("example.bin", "ab") as file:
# Read the existing contents of the file
existing_data = file.read()
print(existing_data)

# Append new data to the file
new_data = b"New data" # converting content into binary
file.write(new_data) # appending content

These different modes provide flexibility in how you can interact with files in Python, allowing you to read, write, append, and create files according to your needs.

--

--

Gopal Khadka

Aspiring coder and writer, passionate about crafting compelling stories through code and words. Merging creativity and technology to connect with others.