Read Data From a File (Solution)

In this lesson, you’ll implement a solution that extends your wordcount command to read data from regular files. You’ll learn how to handle a single command-line argument while retaining the ability to read from standard input, either implicitly or explicitly.

Check Your Current Implementation

At this point, your word count script should resemble the following:

Python src/wordcount.py
import sys

def main():
    raw_text = sys.stdin.buffer.read()
    text = raw_text.decode("utf-8")
    num_lines = text.count("\n")
    num_words = len(text.split())
    num_bytes = len(raw_text)
    max_digits = len(str(max(num_lines, num_words, num_bytes)))
    print(
        f"{num_lines:>{max_digits}} "
        f"{num_words:>{max_digits}} "
        f"{num_bytes:>{max_digits}}"
    )

It can correctly count the number of lines, words, and bytes in standard input, but it can’t yet read data from existing files in your file system. Your task is to modify the script so that it can also read data from a single file specified as a command-line argument. You’ll add the ability to read from multiple files later.

Handle a Command-Line Argument

To start, you’ll need to access command-line arguments passed to your Python script so that you can determine whether a file path was provided. Since you already imported the sys module, you can leverage its sys.argv attribute, which offers a straightforward way to read those command-line arguments.

The sys.argv object is a Python list, whose first element is always the name of the script itself. The remaining elements represent any additional command-line arguments that might have been provided when you executed the script.

To demonstrate this, consider the following example:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.