Search⌘ K
AI Features

Reading and Printing a File

Explore how to handle plain text files in Python by reading and printing their contents. This lesson helps you understand file modes, opening and closing files, and prepares you to build a simple word counting tool based on a real Linux utility.

Linux built-in word count utility

We will begin by building a simple utility called word counter. If you’re familiar with Linux you’ll know this as the wc utility. On Linux, we can type:

wc <filename>

This gets the number of words, lines, and characters in a file. The wc utility is quite advanced since it has been around for a long time. So, we’re going to build a baby version. This will hopefully be more interesting than ...