Reading Files in Python: An Introduction

Techy Bodhisattva
4 min readFeb 27, 2023

--

Reading a file in Python is typically done using the built-in open() function. This function allows us to open any kind of file, such as text files, CSV files, and binary files. The syntax of this function is open(filename, mode). The mode argument specifies the type of access required (read, write, etc.) and the filename is the name of the file you want to open.

In this article, we’ll discuss how to read different types of files in Python. We’ll go over examples for reading text files, CSV files, and binary files.

Text Files

Text files are the most common type of file and can be read by most text editors and word processors. To read a text file in Python, use the open() function with the ‘r’ mode argument. This argument specifies that the file should be opened in read-only mode.

For example, the following code reads a file called “test.txt” in read-only mode:

# Open the file in read-only mode 
f = open("test.txt", "r")

# Read the contents of the file
contents = f.read()

# Close the file
f.close()

print(contents)

This code will open the “test.txt” file and read its contents. The contents of the file are then printed to the screen.

CSV Files

CSV files are comma-separated value files that are often used to store data in a tabular format. To read a CSV file in Python, use the open() function with the ‘r’ mode argument and the csv module.

The csv module provides functions for reading and writing CSV files. When reading a CSV file, the csv module provides the reader() function, which can be used to create a reader object. This object can be iterated over to read the contents of the CSV file.

For example, the following code reads a CSV file called “example.csv” and prints the contents to the screen:

# Import the csv module 
import csv

# Open the file in read-only mode
f = open("example.csv", "r")

# Create a reader object
reader = csv.reader(f)

# Iterate over the rows of the file
for row in reader:
# Print the contents of each row
print(row)

# Close the file
f.close()

This code will open the “example.csv” file and iterate over each row of the file. The contents of each row are then printed to the screen.

Binary Files

Binary files are computer files that are not human-readable. They are often used for storing data in a compact format. To read a binary file in Python, use the open() function with the ‘rb’ mode argument. This argument specifies that the file should be opened in read-only binary mode.

For example, the following code reads a binary file called “example.bin” in read-only binary mode:

# Open the file in read-only binary mode 
f = open("example.bin", "rb")

# Read the contents of the file
contents = f.read()

# Close the file
f.close()

print(contents)

This code will open the “example.bin” file and read its contents. The contents of the file are then printed to the screen.

JSON Files

JSON files are JavaScript Object Notation files that are often used for storing data in a structured format. To read a JSON file in Python, use the open() function with the ‘r’ mode argument and the json module.

The json module provides functions for reading and writing JSON files. When reading a JSON file, the json module provides the load() function, which can be used to create a JSON object. This object can be used to access the contents of the JSON file.

For example, the following code reads a JSON file called “example.json” and prints the contents to the screen: # Import the json module import json

# Open the file in read-only mode 
f = open("example.json", "r")

# Load the contents of the file
data = json.load(f)

# Close the file
f.close()

print(data)

This code will open the “example.json” file and load its contents into a JSON object. The contents of the file are then printed to the screen.

XML Files

XML files are eXtensible Markup Language files that are often used for storing data in a structured format. To read an XML file in Python, use the open() function with the ‘r’ mode argument and the xml module. The xml module provides functions for reading and writing XML files. When reading an XML file, the xml module provides the parse() function, which can be used to create an ElementTree object. This object can be used to access the contents of the XML file.

For example, the following code reads an XML file called “example.xml” and prints the contents to the screen:

# Import the xml module 
import xml

# Open the file in read-only mode
f = open("example.xml", "r")

# Parse the contents of the file
tree = xml.parse(f)

# Close the file
f.close()

# Print the contents of the file
print(tree)

This code will open the “example.xml” file and parse its contents into an ElementTree object. The contents of the file are then printed to the screen.

In this article, we discussed how to read different types of files in Python. We went over examples for reading text files, CSV files, binary files, JSON files, and XML files. We hope this article has been helpful in understanding how to read different types of files in Python.

--

--

Techy Bodhisattva

Women in Tech | There is no limit to what we, as women, can accomplish