Photo from Wikimedia

Reading Files in Python

Mo Cham
3 min readMar 8, 2022

--

Whether working on your programming assignment or working as a data scientist in the biggest software company out there, we can all agree that reading a file in your favorite programming language is one of the most common tasks.

It might be from reading a simple TXT file to analyzing a big CSV file for your next data science project. In this article, we will run down steps included in reading different file types in python.

Computerhope.com simply defines a file as an object on a computer that stores data, information, settings, or commands used with a computer program and a file path as the complete location or name of where a computer, file, device, or web page is located and file extension as the series of characters after a period which indicates the file type.

In this tutorial, we will focus on reading the most common file types with regards to programming, This includes TXT, CSV, JSON, and XML files.

/

├── files/ ← Our current working directory
│ └── cats.txt ← Our TXT file
│ └── cats.csv ← Our CSV file
│ └── cats.json ← Our JSON file
│ └── cats.xml ← Our XML file
└── script.py

Above is our folder structure and to know our working directory, we can always open our folder in the terminal and run pwd command to show the current working directory.

open() this function takes multiple parameters but for the focus of our article, we will focus on two parameters, the 1st one being the file path and the second one being the method or mode for opening the file.

open(file_path, mode)

In our case, the file path will be "files/" since our files and script are located in the same main directory and the mode we will be using is "r" which means to read a file and returns an error if the file doesn't exist.

read() — reads data and returns it as a string. Takes a size parameter to determine how many bytes to return. -1 by default to return the whole file.

readline() — reads a single line as a string.

readlines() — read all the lines file and return them as a list of strings.

close() — the close method closes the file we are working on. It is a good practice to always close files after working with them as this saves memory and avoid files from getting corrupted in case of a program crash.

TXT file

f = open('files/data.txt', 'r')
print(f.read())
f.close()

CSV File

import csv
with open('files/data.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
print(row)

JSON File

import json
with open('files/data.json') as json_file:
data = json.load(json_file)
print(data)

XML File

from logging import root
import xml.etree.ElementTree as ET
content = ET.parse('files/data.xml')
root = content.getroot()
for child in root:
print(child.tag, child.text)

You can find the codes and files on my GitHub: https://github.com/mmodoucham/Python-File-Read/

--

--